Difference between revisions of "TextBackground"

From Wiki
Jump to navigation Jump to search
m
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
< [[Main Page]] | [[Visuals]] >
 
 
 
{{todo|Add more examples of usage of textbackgrounds.}}
 
{{todo|Add more examples of usage of textbackgrounds.}}
  
Pages, paragraphs, words can have a background: color or screen. Note that screen is depricated in MKIV.
+
Pages, paragraphs, words can have a background color or frame. ConTeXt creates these using [[MetaPost]].
 
 
(See, eventually, the [[manual:details.pdf| details]] manual for further explanations.)
 
 
 
== What if textbackground doesn't work? ==
 
  
The textbackground commands require [[write18]] to be enabled; see that page for more information.
+
(See, eventually, the [[manual:details.pdf| details]] manual or the {{src|anch-bck.mkvi|source}} for further explanations.)
  
 
== Example: multi-page backgrounded text ==
 
== Example: multi-page backgrounded text ==
Line 19: Line 13:
 
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=color,backgroundcolor=middlegray,
+
  background=color,
        leftoffset=.5\bodyfontsize,rightoffset=.5\bodyfontsize,
+
  backgroundcolor=lightgray,
        topoffset=.5\bodyfontsize,bottomoffset=.5\bodyfontsize,
+
  leftoffset=.5\bodyfontsize,
        before={\startnarrower\switchtobodyfont[small]},
+
  rightoffset=.5\bodyfontsize,
        after={\stopnarrower},
+
  topoffset=.5\bodyfontsize,
        frame=off]
+
  bottomoffset=.5\bodyfontsize,
 
+
  before={\startnarrower\switchtobodyfont[small]},
\starttext
+
  after={\stopnarrower},
 +
  frame=off,]
  
 
\input knuth
 
\input knuth
  
 
\startsecondary
 
\startsecondary
\input knuth
+
\input knuth
 
\stopsecondary
 
\stopsecondary
  
 
\input knuth
 
\input knuth
 
\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">
\runMPgraphicsfalse %Needed because the wiki runs MkII and write18 is not enabled
 
 
\setupcolors[state=start]
 
 
 
\definetextbackground[secondary][
 
\definetextbackground[secondary][
        location=paragraph,
+
  location=paragraph,
        background=color,backgroundcolor=middlegray,
+
  background=color,
        backgroundoffset=.5\bodyfontsize,
+
  backgroundcolor=lightgray,
        before={\startnarrower\switchtobodyfont[small]},
+
  backgroundoffset=.5\bodyfontsize,
        after={\stopnarrower},
+
  before={\startnarrower\switchtobodyfont[small]},
        frame=off]
+
  after={\stopnarrower},
 
+
  frame=off,]
\starttext
 
  
 
\input knuth
 
\input knuth
  
 
\startsecondary
 
\startsecondary
\input knuth
+
\input knuth
 
\stopsecondary
 
\stopsecondary
  
 
\input knuth
 
\input knuth
 +
</context>
 +
 +
== Underlining with Text Backgrounds ==
 +
 +
Underlining is generally frowned upon by experienced TeXnicians. Occasionally,
 +
however, it is necessary and <cmd>ul</cmd> or <cmd>underbar</cmd> doesn’t do what you want. In those
 +
cases using  <cmd>definetextbackground</cmd> 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]
  
\stoptext
+
An example with \startunderline underlined\stopunderline\ text.
 
</context>
 
</context>
  
== Example: dashed underbar (by WS, 4/2011) ==
+
If desired, the location of the line can also be adjusted.
  
 
<context source="yes">
 
<context source="yes">
\runMPgraphicsfalse % This is needed because the wiki runs MkII and write18 is not enabled.
+
\startuseMPgraphic{mpos:par:columnset} % Shift underlining down a touch
\setuppapersize[B5]
+
  \iftracepositions show_multi_pars \else draw_multi_pars \fi ;
\setupbodyfont[10pt]
+
  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]
 +
An example with \startunderline underlined\stopunderline\ text.
 +
</context>
 +
 
  
 +
=== Example: dashed underbar ===
 +
(by WS, 4/2011)
 +
 +
<context source="yes">
 
\definetextbackground
 
\definetextbackground
 
   [dashed]
 
   [dashed]
   [   location=text,
+
   [location=text,
    framecolor=black,
+
  framecolor=black,
 
   alternative=1,
 
   alternative=1,
      voffset=-\strutdp,
+
  voffset=-\strutdp,
    background=,
+
  background=,
        frame=off,
+
  frame=off,
          dash=1]
+
  dash=1]
  
\starttext
+
\startdashed
  \startdashed\input ward \stopdashed
+
\input ward
\stoptext
+
\stopdashed
 
</context>
 
</context>
 +
 +
== What if textbackground doesn’t work? (MkII) ==
 +
 +
The textbackground commands require [[Write18]] to be enabled; see that page for more information.
 +
 +
[[Category:Basics]]

Revision as of 19:25, 8 June 2020


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


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

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

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,]

\input knuth

\startsecondary
\input knuth
\stopsecondary

\input knuth

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,]

\input knuth

\startsecondary
\input knuth
\stopsecondary

\input knuth

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]

An example with \startunderline underlined\stopunderline\ text.

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]
An example with \startunderline underlined\stopunderline\ text.


Example: dashed underbar

(by WS, 4/2011)

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

\startdashed
\input ward
\stopdashed

What if textbackground doesn’t work? (MkII)

The textbackground commands require Write18 to be enabled; see that page for more information.