4

I was just wandering if there is a package (or an option to a package, like \usepackage[showframe]{geometry}) that could emphasize/draw borders(/lines/frame) around each (and every) word; and/or each line.

I'd prefer if this could work with lipsum (noting that something like \uline{\lipsum[1-10]} [via ulem package] crashes LaTeX). As the title says, I'd primarily need this for debugging purposes (e.g., say, seeing why and where rows do not align in a two-column document).

EDIT: found something similar in macros - Iterate over space-separated list - TeX - LaTeX - Stack Exchange;

\documentclass{article}

\makeatletter
\def\mboxed#1{%
    \@mboxed#1 \@empty
}
\def\@mboxed#1 #2{%
   \fbox{#1}\space  % fbox here to have a visual test
   \ifx #2\@empty\else
    \expandafter\@mboxed
   \fi
   #2%
}
\makeatother

\begin{document}

\def\ptest{Hello there, some words here.}
\mboxed{This should be tested \ptest}

\end{document}

... however, you will notice that only the 'verbatim' words in the \mboxed{} argument are boxed individually; while the contents of the command \ptest are boxed as a whole (and I want them too as individual words; think as using \lipsum instead); furthermore, there is no guarantee that this \mboxed macro doesn't disturb the typesetting (that would occur, if \mboxed wasn't present at all).

2
  • 1
    Are you concerned about hyphenation of words at line ends?
    – Werner
    Aug 28, 2011 at 23:15
  • @Werner - I guess so, because I'd basically just like to follow the text as it is typeset usually/normally (that is, without the [eventual] effects of 'underline/frame everything' macro) ... Many thanks for the feedback - cheers!
    – sdaau
    Aug 28, 2011 at 23:18

4 Answers 4

5

Here's a slightly modified version taken from Censor text spanning multiple lines using LaTeX:

\documentclass{article}
\begin{document}

\def\ptest{Hello there, some words here.}

\renewcommand{\fboxsep}{1pt}%
\def\boxwords#1{\boxloopword#1 \nil}
\def\boxloopword#1 #2\nil{%
  \fbox{\strut#1} % <- Note the space!
  \ifx&#2&% #2 is empty, then & equals &
    \let\next\relax
  \else
    \def\next{\expandafter\boxloopword#2\nil}% iterate
  \fi
  \next\ignorespaces}

\sloppy

\noindent \textbf{Boxed paragraph:} \medskip

\boxwords{Lorem ipsum dolor sit amet, \ptest{} consectetur adipiscing elit. Suspendisse massa lorem, malesuada 
ac imperdiet nec, adipiscing ut lorem. Nunc dignissim nibh erat. Phasellus fermentum lectus 
ut mi viverra a pulvinar turpis interdum. Maecenas mollis laoreet consequat. Etiam malesuada 
ultrices blandit. Praesent sem felis, consectetur eu consectetur id, tempor id quam. Cras id 
lectus vitae dolor varius laoreet. Suspendisse nec metus non justo iaculis ultricies quis 
vel enim. In porttitor dictum orci sit amet feugiat. Praesent pellentesque odio eu orci 
interdum pulvinar. Suspendisse potenti. Mauris lacus lacus, congue tincidunt condimentum 
vitae, elementum quis nibh. Cum sociis natoque penatibus et magnis dis parturient montes, 
nascetur ridiculus mus. Nulla venenatis iaculis mauris, vitae semper neque ullamcorper et. 
Integer et sem eu enim egestas pretium. Sed nulla sapien, pretium eget viverra ut, sollicitudin 
tempor urna. In accumsan euismod augue at sagittis. Sed molestie tincidunt erat eu suscipit. 
Nullam placerat, ipsum a facilisis venenatis, metus est adipiscing augue, at pharetra enim 
dui sit amet metus.}

\bigskip

\noindent \textbf{Unboxed paragraph:} \medskip

Lorem ipsum dolor sit amet, \ptest{} consectetur adipiscing elit. Suspendisse massa lorem, malesuada 
ac imperdiet nec, adipiscing ut lorem. Nunc dignissim nibh erat. Phasellus fermentum lectus 
ut mi viverra a pulvinar turpis interdum. Maecenas mollis laoreet consequat. Etiam malesuada 
ultrices blandit. Praesent sem felis, consectetur eu consectetur id, tempor id quam. Cras id 
lectus vitae dolor varius laoreet. Suspendisse nec metus non justo iaculis ultricies quis 
vel enim. In porttitor dictum orci sit amet feugiat. Praesent pellentesque odio eu orci 
interdum pulvinar. Suspendisse potenti. Mauris lacus lacus, congue tincidunt condimentum 
vitae, elementum quis nibh. Cum sociis natoque penatibus et magnis dis parturient montes, 
nascetur ridiculus mus. Nulla venenatis iaculis mauris, vitae semper neque ullamcorper et. 
Integer et sem eu enim egestas pretium. Sed nulla sapien, pretium eget viverra ut, sollicitudin 
tempor urna. In accumsan euismod augue at sagittis. Sed molestie tincidunt erat eu suscipit. 
Nullam placerat, ipsum a facilisis venenatis, metus est adipiscing augue, at pharetra enim 
dui sit amet metus.

\end{document}

You'll notice the different typesetting of each paragraph. Using \fbox removes hyphenation. Also, using \expandafter\boxloopword allows for the expansion of a macro within the \boxwords argument (like your \ptest example).

Blocked words

Perhaps the other solution using the soul package (also at the same link) would be helpful.

7
  • 1
    \fbox{\strut#1} with a fboxsep=1pt looks nicer
    – user2478
    Aug 29, 2011 at 6:35
  • @Herbert: Agreed.
    – Werner
    Aug 29, 2011 at 6:41
  • Hi @Werner - many thanks for the example! Putting the accept here for now since it works in Latex, even if I am not too fond of "...the different typesetting of each paragraph. Using \fbox removes hyphenation.". Note that \boxwords{\ptest} still boxes entire content; as long as there is but one space, \boxwords{a \ptest} then individual words are boxed. Btw, I was trying the exact same example, the "\expandafter\boxloopword allows for the expansion" part was what I waas missing :) Many thanks again - cheers!
    – sdaau
    Aug 29, 2011 at 7:09
  • Sorry, kinda late with this question - but I was wandering, what does '\def\do#1{#1}' actually do? I cannot see any sort of \doXXX command being called anywhere... Thanks!
    – sdaau
    Aug 29, 2011 at 8:12
  • 1
    @Amar: Perhaps you can follow Is it possible to make LaTeX mark overfull boxes in the output?
    – Werner
    Sep 30, 2016 at 16:06
3

I don't know of a LaTeX solution, but am pointing out a few ConTeXt solutions, incase someone wants to copy the code and implement them in LaTeX.

For framing each word, you can use \processwords macro. See page 32 of the metafun manual.

For underlining individual words, you can use \underbars macro; for underlining individual lines, you can use \underbar macros.

All three work fine with multi-paragraph arguments. Here is a minimal example showing usage:

\let\processword\framed                                                                                                                

\starttext
\startTEXpage
\processwords{\input knuth \endgraf}
\blank[big]
\underbars{\input knuth \endgraf}
\blank[big]
\underbar{\input knuth \endgraf}
\stopTEXpage
\stoptext

which gives

enter image description here

For visual debugging, ConTeXt also provides a \showboxes macro that displays all the boxes on the page. See Hans Hagen's 1998 Tugboat article for details. Below is a small example

\setuppapersize[A7]

\showboxes
\showframe

\starttext
\section {First}

\startitemize[n]
    \item One
    \item Two
\stopitemize
\stoptext                         

which gives

enter image description here

EDIT It is possible to adapt the ConTeXt solutions to work in LaTeX, but for that you will need to copy the definitions of different ConTeXt macros that are used. For example, here is how you can define processwords in LaTeX:

\makeatletter
% from syst-ext.mkii
\long\def\ConvertToConstant#1#2#3%
  {\edef\@@stringa{\expandafter\detokenize\expandafter{#2}}%
   \edef\@@stringb{\expandafter\detokenize\expandafter{#3}}%
   #1{\@@stringa}{\@@stringb}}

% from core-fnt.mkii
\def\doprocesswords#1 #2\od
  {\ConvertToConstant\doifnot{#1}{}
     {\processword{#1} %
      % expandafter added
      \expandafter\doprocesswords#2 \od}}


% from core-fnt.mkii
\def\processwords#1%
  {\doprocesswords#1 \od\unskip}

% from syst-gen.mkii
\long\def\doifnot#1#2%
  {\let\donottest\dontprocesstest
   \edef\@@stringa{#1}%
   \edef\@@stringb{#2}%
   \let\donottest\doprocesstest
   \ifx\@@stringa\@@stringb
     \expandafter\gobbleoneargument
   \else
     \expandafter\firstofoneargument
   \fi}

\long\def\gobbleoneargument#1{}
\long\def\firstofoneargument#1{#1}


\makeatother




\def\ptest{Hello there, some words here.}

and then use it as:

\renewcommand{\fboxsep}{1pt}%
\let\processword\fbox

\documentclass{minimal}

\begin{document}
\processwords{Lorem ipsum dolor sit amet, \ptest{} consectetur adipiscing elit.
Suspendisse massa lorem, malesuada ac imperdiet nec, adipiscing ut lorem. Nunc
dignissim nibh erat. Phasellus fermentum lectus ut mi viverra a pulvinar turpis
interdum. Maecenas mollis laoreet consequat. Etiam malesuada ultrices blandit.
Praesent sem felis, consectetur eu consectetur id, tempor id quam. Cras id
lectus vitae dolor varius laoreet. Suspendisse nec metus non justo iaculis
ultricies quis vel enim. In porttitor dictum orci sit amet feugiat. Praesent
pellentesque odio eu orci interdum pulvinar. Suspendisse potenti. Mauris lacus
lacus, congue tincidunt condimentum vitae, elementum quis nibh. Cum sociis
natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nulla
venenatis iaculis mauris, vitae semper neque ullamcorper et. Integer et sem eu
enim egestas pretium. Sed nulla sapien, pretium eget viverra ut, sollicitudin
tempor urna. In accumsan euismod augue at sagittis. Sed molestie tincidunt erat
eu suscipit. Nullam placerat, ipsum a facilisis venenatis, metus est adipiscing
augue, at pharetra enim dui sit amet metus.}

\end{document}
3
  • Thank you @Aditya - that is exactly the kind of thing I needed... As I don't use ConText, I will wait a bit to see if someone maybe comes up with anything a bit more straightforward for Latex - otherwise, this is more than acceptable as solution; many thanks for mentioning \showboxes too... Many thanks, cheers!
    – sdaau
    Aug 29, 2011 at 5:42
  • Just a note - for those that have never seen Context before, Berend de Boer "LaTeX in proper ConTeXt". 2003 (pdf) seems a nice tutorial (via ConTeXt - Wikipedia, the free encyclopedia)
    – sdaau
    Aug 29, 2011 at 8:01
  • Also, many thanks for your edit, @Aditya - it was really helpful with a working example of a Context macro copied in Latex ! Cheers!
    – sdaau
    Aug 29, 2011 at 22:09
1

This is not exactly for individual words, but it is related (as it prints out layout boxes) - from LaTeX book class: Twosided document with wrong margins #2565797 - Stack Overflow:

In general, to see what's going on with your layout, you can put \usepackage{layout} in your preamble, and then stick \layout in your document to get a diagram and listing of geometry settings.

I'm guessing this is built in...

Then, on CTAN, there is this layouts package - that one can apparently also draw boxes around words (see layman.pdf), but haven't tried that package yet...

0

Thanks to the edit in @Aditya's answer; finally I have an example that visualizes lipsum paragraphs in two column mode. I've simply used \let\processword\uline to have the words underlined instead of boxed. The below example also simultaneously activates \showmakeup:

\documentclass[twocolumn]{article}
\usepackage[nopar]{lipsum} % MUST be nopar!
\usepackage[normalem]{ulem} % for \uline underline

% https://tex.stackexchange.com/questions/26806/expanding-edef-a-lipsum-command
\makeatletter
\def\unpacklipsum#1#2#3{%
  \count@=#1\relax
  \advance\count@\m@ne
  \def#3{}%
  \loop\ifnum\count@<#2\relax
    \advance\count@\@ne
    \edef#3{#3\csname lipsum@\romannumeral\count@\endcsname}%
  \repeat}
\makeatother


\makeatletter
% from syst-ext.mkii
\long\def\ConvertToConstant#1#2#3%
  {\edef\@@stringa{\expandafter\detokenize\expandafter{#2}}%
   \edef\@@stringb{\expandafter\detokenize\expandafter{#3}}%
   #1{\@@stringa}{\@@stringb}}

% from core-fnt.mkii
\def\doprocesswords#1 #2\od
  {\ConvertToConstant\doifnot{#1}{}
     {\processword{#1} %
      % expandafter added
      \expandafter\doprocesswords#2 \od}}

% from core-fnt.mkii
\def\processwords#1%
  {\doprocesswords#1 \od\unskip}

% from syst-gen.mkii
\long\def\doifnot#1#2%
  {\let\donottest\dontprocesstest
   \edef\@@stringa{#1}%
   \edef\@@stringb{#2}%
   \let\donottest\doprocesstest
   \ifx\@@stringa\@@stringb
     \expandafter\gobbleoneargument
   \else
     \expandafter\firstofoneargument
   \fi}

\long\def\gobbleoneargument#1{}
\long\def\firstofoneargument#1{#1}

\makeatother

% -----

\renewcommand{\fboxsep}{1pt}%
% \let\processword\fbox
% \let\processword\underbar
\let\processword\uline

\begin{document}


\let\unexpanded\protected
\input{norm-tex.mkii} % upon \showmakeup - \normalvfil required
\input{supp-vis.mkiv}
\showmakeup


\unpacklipsum{1}{1}{\myflattenedlipsum}

% note: \processwords{\myflattenedlipsum} typesets in single line!
% must have at least a letter and a space, to cause per-word processing
% HOWEVER, \processwords{a \lipsum[1-2]} will *still* be typeset as one line!
% \processwords{a \myflattenedlipsum}

\processwords{P \myflattenedlipsum}

\unpacklipsum{2}{2}{\myflattenedlipsum}
\processwords{P \myflattenedlipsum}

\unpacklipsum{3}{4}{\myflattenedlipsum}
\processwords{P \myflattenedlipsum}


\end{document}

... and so results with the following, somewhat heavy rendering:

twoculine.png

.... but at least I have a point of reference now, and a visual indication of row alignment in two column mode - but at the following expense:

  • \lipsum paragraphs need to be expanded, and they cannot use \par (so paragraphs must be manually reconstructed)
  • Additionally, must add at least a letter to each \lipsum paragraph (to 'unpack' individual words)
  • Typesetting according to hyphenation is not preserved.

One would think that it should be possible to capture the Tex box that represents a given row/line and simply fbox it (thus preserving hyphenation), but I tried looking at \tracingall, and I cannot see anything like that :) As for copying Context macros, I tried to copy in the macro for \underbars - but that one is apparently a bit more complex; and there I'd get stuck with macros looking like \!!width, which are apparently something Context-specific (meant for luatex maybe?), as Latex cannot seem to parse them; but I can see in @Aditya's edit, the !! can be simply replaced with @@ for Latex.


Right, the earlier example I leave for reference (way below) - however, it actually does nothing; it uses seemingly the wrong \underbar.

The good thing is - the Context visualisation macros (supp-vis.mkiv) can indeed be used with plain (la)tex; unfortunately, the \underbar(s) commands that underline each word/line are part of the core-fnt.mkii/mkiv Context file, which requires a ton of Lua scripts - and I couldn't cheat it even with \usepackage{luatex} and running lualatex.

Nonetheless, here is a tex example which will use the Context visualisation macro, and is compilable with pdflatex only:

\documentclass{article}
\usepackage[nopar]{lipsum}

% https://tex.stackexchange.com/questions/26806/expanding-edef-a-lipsum-command
\makeatletter
\def\unpacklipsum#1#2#3{%
  \count@=#1\relax
  \advance\count@\m@ne
  \def#3{}%
  \loop\ifnum\count@<#2\relax
    \advance\count@\@ne
    \edef#3{#3\csname lipsum@\romannumeral\count@\endcsname}%
  \repeat}
\makeatother

\begin{document}

\let\unexpanded\protected
\input{norm-tex.mkii} % upon \showmakeup - \normalvfil required
\input{supp-vis.mkiv}
\showmakeup
% \showboxes % a bit tinier than \showmakeup
% \showframe % not in supp-vis

\unpacklipsum{1}{1}{\mycommand}

a \mycommand

\end{document}

(the output is similar to "visual debugging" example image of \showboxes macro in @Aditya's answer)


OK, here's my attempt to use @Aditya's answer in Latex; after installing Context (tlmgr install context); I was just trying to load some of its files "as if" they were packages - I got somewhere, with only partial success (can underline text; but only in a single line)... If anyone has an idea if it is impossible to go along this approach, that would be great to know.

Anyways, here is, say, 'test-con.tex':

\documentclass{article}

\usepackage[nopar]{lipsum}

% https://tex.stackexchange.com/questions/26806/expanding-edef-a-lipsum-command
\makeatletter
\def\unpacklipsum#1#2#3{%
  \count@=#1\relax
  \advance\count@\m@ne
  \def#3{}%
  \loop\ifnum\count@<#2\relax
    \advance\count@\@ne
    \edef#3{#3\csname lipsum@\romannumeral\count@\endcsname}%
  \repeat}
\makeatother

% \usepackage{contextcopy} % ! LaTeX Error: Missing \begin{document}. at l.740 \newskip \centering  \centering....
% same Missing \begin{document} for below as well:
% \makeatletter
% \input{contextcopy.sty}
% \makeatother


\begin{document}

\makeatletter
\input{contextcopy.sty}
\makeatother

\unpacklipsum{1}{1}{\mycommand}

\underbar{Here \mycommand}

\end{document}

 

... and here is 'contextcopy.sty':

% \input{/path/to/texlive/2011/texmf-dist/tex/context/base/supp-mis.mkii} % auto-read
\input{/path/to/texlive/2011/texmf-dist/tex/context/base/supp-pdf.mkii}
\input{/path/to/texlive/2011/texmf-dist/tex/context/base/syst-ini.mkii}

% \let\unexpanded\normalprotected % as in syst-aux.mkiv gives: ! Undefined control sequence.
% \let\unexpanded\relax %
\let\unexpanded\protect % same effect as \relax
\input{/path/to/texlive/2011/texmf-dist/tex/context/base/supp-vis.mkiv}

 

Funnily, even if I use pdflatex, with this setup, it is a dvi that gets generated:

$ pdflatex test-con.tex
This is pdfTeX, Version 3.1415926-2.3-1.40.12 (TeX Live 2011)
...
) (/path/to/texlive/2011/texmf-dist/tex/context/base/supp-vis.mkiv
loading: ConTeXt Support Macros / Visualization
))
Overfull \hbox (3454.36774pt too wide) in paragraph at lines 133--134
 []$[]$ 
[1] [2] (./test-con.aux) )
(see the transcript file for additional information)
Output written on test-con.dvi (2 pages, 1292 bytes).
Transcript written on test-con.log.

 

... and here is how xdvi renders it:

xdvi-test.png

 

Obviously, if it was possible to somehow load just the visualization stuff from Context, and otherwise build w/ plain pdflatex, such that underlined multi-paragraph text is rendered, that would be awesome :) But I cannot tell if this sort of approach would eventually work..

Thanks again for all the responses,
Cheers!

2
  • 1
    I am afraid that such an ConTeXt is not modular enough that you can just input bits and pieces of it. It is difficult to explain in this comment, so I'll post another solution.
    – Aditya
    Aug 29, 2011 at 15:38
  • 1
    Thanks for clearing that up, @Aditya - I was trying to play around a bit with it, and for instance, Context file supp-vis.mkiv has in comments, encouragingly: "Although an integral part of \CONTEXT, this module is one of the support modules. Its stand alone character permits use in \PLAIN\ \TEX\ or \TEX\ based macropackages."; and then few lines below, discouragingly: "Most of those macros can be found in \type {core-vis} and other core modules. Their integration in \CONTEXT\ prohibits generic applications." :) ... Thanks for the help - cheers!
    – sdaau
    Aug 29, 2011 at 15:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .