Difference between revisions of "TextBackground"

From Wiki
Jump to navigation Jump to search
(Created page.)
 
(more intro)
 
(20 intermediate revisions by 9 users not shown)
Line 1: Line 1:
< [[Main Page]] | [[Visuals]] >
+
{{todo|Add more examples of usage of textbackgrounds.}}
 +
 
 +
== Basics ==
 +
 
 +
Pages, paragraphs, words can have a background color or [[Overlays|overlay]]. ConTeXt creates these using [[MetaPost]].
 +
 
 +
But a usual frame is only one object, its contents can’t flow over to the next page.
 +
 
 +
You can use {{cmd|definetextbackground}} to define a named text background. The environment is based on {{cmd|framed}} and understands its parameters, but can contain several paragraphs and page breaks.
 +
 
 +
After `\definetextbackground[Name]` you can use `\startName ... \stopName` (if there’s no name conflict) or `\starttextbackground[Name] ... \stoptextbackground`. Usually you won’t need {{cmd|setuptextbackground}}.
 +
 
 +
The `location` parameter changes the behaviour between `text` (highlighting running text) and `paragraph` (boxed paragraphs).
 +
 
 +
(See, eventually, the [[manual:details.pdf| details]] manual or the {{src|anch-bck.mkvi|source}} for further explanations.)
  
{{todo|Add more examples of usage of textbackgrounds.}}
 
  
Pages, paragraphs, words can have a background: color or screen.
+
'''Beware''': The examples using {{cmd|startnarrower}} may be misleading – of course you can use {{cmd|startbackground}} without the `narrower` environment; it behaves differently without `\starttext ... \stoptext`.
  
 
== Example: multi-page backgrounded text ==
 
== Example: multi-page backgrounded text ==
  
Perhaps one might like to offset examples from the running text, or signal to the reader that the following paragraphs contain secondary, detailed information that is of interest only to the specialists. This can be achieved using textbackgrounds.
+
Perhaps one might like to offset examples from the running text, or signal to the reader that the following paragraphs contain secondary, detailed information that is of interest only to the specialists. This can be achieved using textbackground.
  
 
In contrast, [[Framed]] creates text blocks that do not span columns or pages.
 
In contrast, [[Framed]] creates text blocks that do not span columns or pages.
Line 13: Line 26:
 
In the following example, we also reduce the textwidth and bodyfont size.
 
In the following example, we also reduce the textwidth and bodyfont size.
 
<context source="yes">
 
<context source="yes">
\setupcolors[state=start]
 
 
 
\definetextbackground[secondary][
 
\definetextbackground[secondary][
        location=paragraph,
+
  location=paragraph,
        %background=screen, %doesn't work??
+
  background=color,
background=color,backgroundscreen=middlegray,
+
  backgroundcolor=lightgray,
        leftoffset=.5\bodyfontsize,rightoffset=.5\bodyfontsize,
+
  leftoffset=.5\bodyfontsize,
        topoffset=.5\bodyfontsize,bottomoffset=.5\bodyfontsize,
+
  rightoffset=.5\bodyfontsize,
        before={\startnarrower\switchtobodyfont[small]},
+
  topoffset=.5\bodyfontsize,
        after={\switchtobodyfont[normal]\stopnarrower},
+
  bottomoffset=.5\bodyfontsize,
        frame=off]
+
  before={\startnarrower\switchtobodyfont[small]},
 +
  after={\stopnarrower},
 +
  frame=off,]
  
 
\starttext
 
\starttext
 
+
\samplefile{knuth}
\input knuth
 
  
 
\startsecondary
 
\startsecondary
\input knuth
+
\samplefile{knuth}
 
\stopsecondary
 
\stopsecondary
  
\input knuth
+
\samplefile{knuth}
 
 
 
\stoptext
 
\stoptext
 
</context>
 
</context>
  
{{todo|The background does not show here using the live, online conTeXt processor. Why?}}
+
== Background Offset ==
 
 
== backgroundoffset ==
 
  
 
The options left, right, top, bottomoffset can be replaced by backgroundoffset yielding a different result:
 
The options left, right, top, bottomoffset can be replaced by backgroundoffset yielding a different result:
  
 
<context source="yes">
 
<context source="yes">
\setupcolors[state=start]
 
 
 
\definetextbackground[secondary][
 
\definetextbackground[secondary][
        location=paragraph,
+
  location=paragraph,
        %background=screen, %doesn't work??
+
  background=color,
background=color,backgroundscreen=middlegray,
+
  backgroundcolor=lightgray,
        backgroundoffset=.5\bodyfontsize,
+
  backgroundoffset=.5\bodyfontsize,
        before={\startnarrower\switchtobodyfont[small]},
+
  before={\startnarrower\switchtobodyfont[small]},
        after={\switchtobodyfont[normal]\stopnarrower},
+
  after={\stopnarrower},
        frame=off]
+
  frame=off,]
  
 
\starttext
 
\starttext
 
+
\samplefile{knuth}
\input knuth
 
  
 
\startsecondary
 
\startsecondary
\input knuth
+
\samplefile{knuth}
 
\stopsecondary
 
\stopsecondary
  
\input knuth
+
\samplefile{knuth}
 +
\stoptext
 +
</context>
 +
 
 +
== Underlining with Text Backgrounds ==
  
\stoptext
+
Underlining is generally frowned upon by experienced TeXnicians. Occasionally,
 +
however, it is necessary and {{cmd|ul}} or {{cmd|underbar}} doesn’t do what you want. In those
 +
cases using {{cmd|definetextbackground}} for the underlining may also be tried.
 +
 
 +
<context source="yes">
 +
\definetextbackground[underline]
 +
  [location=text,alternative=1,background=,frame=off]
 +
 
 +
\definestartstop[underline]
 +
  [before={\starttextbackground[underline]},
 +
  after=\stoptextbackground]
 +
 
 +
\startTEXpage
 +
An example with \startunderline underlined\stopunderline\ text.
 +
\stopTEXpage
 +
</context>
 +
 
 +
If desired, the location of the line can also be adjusted.
 +
 
 +
<context source="yes">
 +
\startuseMPgraphic{mpos:par:columnset} % Shift underlining down a touch
 +
  \iftracepositions show_multi_pars \else draw_multi_pars \fi ;
 +
  path p ; p := boundingbox currentpicture ;
 +
  currentpicture := currentpicture shifted (0,-StrutDepth/3) ;
 +
  setbounds currentpicture to p ;
 +
\stopuseMPgraphic
 +
 
 +
\definetextbackground[underline]
 +
  [location=text,alternative=1,background=,frame=off]
 +
 
 +
\definestartstop[underline]
 +
  [before={\starttextbackground[underline]},
 +
  after=\stoptextbackground]
 +
 
 +
\startTEXpage
 +
An example with \startunderline underlined\stopunderline\ text.
 +
\stopTEXpage
 +
</context>
 +
 
 +
 
 +
=== Example: dashed underbar ===
 +
(by WS, 4/2011)
 +
 
 +
<context source="yes">
 +
\definetextbackground
 +
  [dashed]
 +
  [location=text,
 +
  framecolor=black,
 +
  alternative=1,
 +
  voffset=-\strutdp,
 +
  background=,
 +
  frame=off,
 +
  dash=1]
 +
 
 +
\startdashed
 +
\samplefile{ward}
 +
\stopdashed
 
</context>
 
</context>
 +
 +
[[Category:Basics]]

Latest revision as of 19:03, 6 December 2023


TODO: Add more examples of usage of textbackgrounds. (See: To-Do List)


Basics

Pages, paragraphs, words can have a background color or overlay. ConTeXt creates these using MetaPost.

But a usual frame is only one object, its contents can’t flow over to the next page.

You can use \definetextbackground to define a named text background. The environment is based on \framed and understands its parameters, but can contain several paragraphs and page breaks.

After \definetextbackground[Name] you can use \startName ... \stopName (if there’s no name conflict) or \starttextbackground[Name] ... \stoptextbackground. Usually you won’t need \setuptextbackground.

The location parameter changes the behaviour between text (highlighting running text) and paragraph (boxed paragraphs).

(See, eventually, the details manual or the source for further explanations.)


Beware: The examples using \startnarrower may be misleading – of course you can use \startbackground without the narrower environment; it behaves differently without \starttext ... \stoptext.

Example: multi-page backgrounded text

Perhaps one might like to offset examples from the running text, or signal to the reader that the following paragraphs contain secondary, detailed information that is of interest only to the specialists. This can be achieved using textbackground.

In contrast, Framed creates text blocks that do not span columns or pages.

In the following example, we also reduce the textwidth and bodyfont size.

\definetextbackground[secondary][
  location=paragraph,
  background=color,
  backgroundcolor=lightgray,
  leftoffset=.5\bodyfontsize,
  rightoffset=.5\bodyfontsize,
  topoffset=.5\bodyfontsize,
  bottomoffset=.5\bodyfontsize,
  before={\startnarrower\switchtobodyfont[small]},
  after={\stopnarrower},
  frame=off,]

\starttext
\samplefile{knuth}

\startsecondary
\samplefile{knuth}
\stopsecondary

\samplefile{knuth}
\stoptext

Background Offset

The options left, right, top, bottomoffset can be replaced by backgroundoffset yielding a different result:

\definetextbackground[secondary][
  location=paragraph,
  background=color,
  backgroundcolor=lightgray,
  backgroundoffset=.5\bodyfontsize,
  before={\startnarrower\switchtobodyfont[small]},
  after={\stopnarrower},
  frame=off,]

\starttext
\samplefile{knuth}

\startsecondary
\samplefile{knuth}
\stopsecondary

\samplefile{knuth}
\stoptext

Underlining with Text Backgrounds

Underlining is generally frowned upon by experienced TeXnicians. Occasionally, however, it is necessary and \ul or \underbar doesn’t do what you want. In those cases using \definetextbackground for the underlining may also be tried.

\definetextbackground[underline]
  [location=text,alternative=1,background=,frame=off]

\definestartstop[underline]
  [before={\starttextbackground[underline]},
   after=\stoptextbackground]

\startTEXpage
An example with \startunderline underlined\stopunderline\ text.
\stopTEXpage

If desired, the location of the line can also be adjusted.

\startuseMPgraphic{mpos:par:columnset} % Shift underlining down a touch
   \iftracepositions show_multi_pars \else draw_multi_pars \fi ;
   path p ; p := boundingbox currentpicture ;
   currentpicture := currentpicture shifted (0,-StrutDepth/3) ;
   setbounds currentpicture to p ;
\stopuseMPgraphic

\definetextbackground[underline]
  [location=text,alternative=1,background=,frame=off]

\definestartstop[underline]
  [before={\starttextbackground[underline]},
   after=\stoptextbackground]

\startTEXpage
An example with \startunderline underlined\stopunderline\ text.
\stopTEXpage


Example: dashed underbar

(by WS, 4/2011)

\definetextbackground
  [dashed]
  [location=text,
   framecolor=black,
   alternative=1,
   voffset=-\strutdp,
   background=,
   frame=off,
   dash=1]

\startdashed
\samplefile{ward}
\stopdashed