Difference between revisions of "Cropping text"

From Wiki
Jump to navigation Jump to search
m (\underline has to be \underbar; you can simply paste some definitions at the top and remove them again after the new version is online)
Line 1: Line 1:
 +
< [[Visuals]] |
 +
 
== Cropping text ==
 
== Cropping text ==
 
There are some rare cases in which it is useful to crop a given
 
There are some rare cases in which it is useful to crop a given
 
text line and loose part of its information.
 
text line and loose part of its information.
  
 +
=== <tt>\doboundtext</tt> ===
 
Sometimes there is not enough room to show the complete
 
Sometimes there is not enough room to show the complete
 
(line of) text. In such a situation we can strip of some
 
(line of) text. In such a situation we can strip of some
Line 27: Line 30:
  
  
 +
=== <tt>\limitatetext</tt> ===
 
A bit more beautiful alternative for the previous command is <tt>\limitatetext</tt>.
 
A bit more beautiful alternative for the previous command is <tt>\limitatetext</tt>.
 
This command takes care of '''word''' boundaries, so that only complete words will
 
This command takes care of '''word''' boundaries, so that only complete words will
Line 60: Line 64:
 
on text that cannot be hyphenated.
 
on text that cannot be hyphenated.
  
 +
 +
=== <tt>\limitatefirstline</tt> ===
 
In ConTeXt versions newer than 07.09.2005 there is also a command
 
In ConTeXt versions newer than 07.09.2005 there is also a command
called <tt>\limitatefirstline</tt>, which acts like <tt>\doboundtext</tt>,
+
called <tt>\limitatefirstline</tt>, which acts nearly like <tt>\doboundtext</tt>,
but at a more robust level.
+
but at a more robust level. This macro first tries to break the given text
 +
at character level. It uses nearly the same breaking mechanism as <tt>\limitatetext</tt>,
 +
so it shares the problem with nonbreakable text.
 +
But in contrast to <tt>\limitatetext</tt> it has a second mechanism, that is only used,
 +
if the prior one fails.
 +
This fail safe mechanism "simply" clips (no breaking at all) the text to the disired
 +
measures. This is done without any consideration to character boundaries.
 +
So you must live with the fact, that the last character can be cut off at any possible
 +
place.
  
 
<texcode>
 
<texcode>
Line 71: Line 85:
  
 
<texcode>
 
<texcode>
\limitatefirstline {\underbar{The garden's version of \CONTEXT is too old.}{65mm}{\unknown}
+
\limitatefirstline {\underbar{Mr.~Drofnats was happiest when he was at work%
 +
typesetting beautiful documents.}{110mm}{\unknown}
 
</texcode>
 
</texcode>
  
Line 78: Line 93:
  
 
\def\limitatefirstline#1#2#3%
 
\def\limitatefirstline#1#2#3%
{\hbox\bgroup\strut
+
{\hbox\bgroup\strut
  \setbox\scratchbox\hbox{#1}%
+
\setbox\scratchbox\hbox{#1}%
  \ifdim\wd\scratchbox>#2\relax
+
\ifdim\wd\scratchbox>#2\relax
    \setbox\scratchbox\hbox{#3}%
+
  \setbox\scratchbox\hbox{#3}%
    \hsize#2\relax
+
  \hsize#2\relax
    \advance\hsize-\wd\scratchbox
+
  \advance\hsize-\wd\scratchbox
    \setbox\scratchbox\vbox{\forgetall\veryraggedright#1}%
+
  \setbox\scratchbox\vbox{\forgetall\veryraggedright#1}%
    \setbox\scratchbox\vsplit\scratchbox to \lineheight
+
  \setbox\scratchbox\vsplit\scratchbox to \lineheight
    \vbox
+
  \vbox
      {\unvbox\scratchbox
+
    {\unvbox\scratchbox
      \global\setbox\plusone\lastbox
+
      \global\setbox\plusone\lastbox
      \global\setbox\plusone\hbox{\unhbox\plusone}%
+
      \global\setbox\plusone\hbox{\unhbox\plusone}%
      \ifx\clip\undefined
+
      \hbox to #2
        \box\plusone\removeunwantedspaces#3%
+
        {\ifx\clip\undefined
      \else\ifdim\wd\plusone>\hsize
+
          \box\plusone
        \hbox{\clip[\c!width=\hsize]{\box\plusone}#3}%
+
        \else\ifdim\wd\plusone>\hsize
      \else
+
          \clip[\c!width=\hsize,\c!height=\lineheight,\c!voffset=-4pt]{\box\plusone}%
        \box\plusone\removeunwantedspaces#3%
+
        \else
      \fi\fi}%
+
          \box\plusone
  \else
+
        \fi\fi
    #1%
+
        \removeunwantedspaces\hss#3}}%
  \fi
+
\else
  \egroup}
+
  #1%
 +
\fi
 +
\egroup}  
  
 
\protect
 
\protect
  
\limitatefirstline {\underbar{The garden's version of \CONTEXT is too old.}}{65mm}{\unknown}
+
\limitatefirstline {\underbar{Mr.~Drofnats was happiest when he was at work typesetting beautiful documents.}}{110mm}{\unknown}
 
</context>
 
</context>
 +
 +
 +
=== A comparison (TODO) ===
 +
comparison table with examples of all commands

Revision as of 13:48, 9 September 2005

< Visuals |

Cropping text

There are some rare cases in which it is useful to crop a given text line and loose part of its information.

\doboundtext

Sometimes there is not enough room to show the complete (line of) text. In such a situation we can strip of some characters by using

\doboundtext {text} {width} {sentinel}

When the text is wider than the given width, it's split and the third argument (sentinel) is appended. As much text as possible is printed.

An example

\doboundtext{My entire inheritance goes to my beloved cat Pussy!}{70mm}{...}

and it's result


\limitatetext

A bit more beautiful alternative for the previous command is \limitatetext. This command takes care of word boundaries, so that only complete words will appear in the final (cropped) text.

\limitatetext {text}  {width} {sentinel}
\limitatetext {text} {-width} {prelude}

When no width is given, the whole text becomes available. A negative value crops the beginning and the text starts with the prelude. Sentinel and prelude are optional.

Example:

\limitatetext {Pussy is the name of the cat!}{50mm}{...}\par
\limitatetext {Pussy is the name of the cat!}{-50mm}{...}

leads to


Both commands have their range of application. \limitatetext is more robust (using grouping tokens like \underbar is possible), while \doboundtext works better on text that cannot be hyphenated.


\limitatefirstline

In ConTeXt versions newer than 07.09.2005 there is also a command called \limitatefirstline, which acts nearly like \doboundtext, but at a more robust level. This macro first tries to break the given text at character level. It uses nearly the same breaking mechanism as \limitatetext, so it shares the problem with nonbreakable text. But in contrast to \limitatetext it has a second mechanism, that is only used, if the prior one fails. This fail safe mechanism "simply" clips (no breaking at all) the text to the disired measures. This is done without any consideration to character boundaries. So you must live with the fact, that the last character can be cut off at any possible place.

\limitatefirstline {text} {width} {sentinel}

Example:

\limitatefirstline {\underbar{Mr.~Drofnats was happiest when he was at work%
 typesetting beautiful documents.}{110mm}{\unknown}


A comparison (TODO)

comparison table with examples of all commands