0

@David Purton commented at How to automatically force latex to not justify the text when it is not wise?

In TeX by Topic (eijkhout.net/texbytopic/texbytopic.html) there is an example of getting lines in a paragraph to be raggedright if they are too underful. See section 5.9.6. Perhaps this method would be suitable?

On that book I found the page:

enter image description here

It does refer to the macro:

\newbox\linebox \newbox\snapbox
\def\eatlines{
\setbox\linebox\lastbox % check the last line
\ifvoid\linebox
\else % if it’s not empty
\unskip\unpenalty % take whatever is
{\eatlines} % above it;
% collapse the line
\setbox\snapbox\hbox{\unhcopy\linebox}
% depending on the difference
\ifdim\wd\snapbox<.90\wd\linebox
\box\snapbox % take the one or the other,
\else \box\linebox \fi
\fi}

Within it we may notice the .98 number, which should refer to 98% of the line being filled with text and 2% as empty spacing due the LaTeX text justification.

Currently I was able to use it as:

% proposal.tex
% Based on http://www.latextemplates.com/template/simple-sectioned-essay
\documentclass[12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[a4paper, margin=2cm]{geometry}
\usepackage[brazil]{babel}

\usepackage{hyphsubst}
\usepackage{mathptmx}

\newbox\linebox \newbox\snapbox
\def\eatlines{
\setbox\linebox\lastbox % check the last line
\ifvoid\linebox
\else % if it’s not empty
\unskip\unpenalty % take whatever is
{\eatlines} % above it;
% collapse the line
\setbox\snapbox\hbox{\unhcopy\linebox}
% depending on the difference
\ifdim\wd\snapbox<.90\wd\linebox
\box\snapbox % take the one or the other,
\else \box\linebox \fi
\fi}

\begin{document}

\section{Riscos}

\indent
\vbox{

    In typesetting advertisement copy, a way of justifying paragraphs has
    become popular in recent years that is somewhere between flushright
    and raggedright setting. Lines that would stretch beyond certain limits
    are set with their glue at natural width. This single paragraph is but an
    example of this procedure; the macros are given next.

\par\eatlines}

\end{document}

Moreover on the text, he does mention about it can be inserted automatically with \everypar. Though, I do not understand how it could be done automatically with \everypar. So far I tried this:

\begin{document}

\section{Riscos}

\indent
\everypar{

    In typesetting advertisement copy, a way of justifying paragraphs has
    become popular in recent years that is somewhere between flushright
    and raggedright setting. Lines that would stretch beyond certain limits
    are set with their glue at natural width. This single paragraph is but an
    example of this procedure; the macros are given next.

}

\end{document}

However the image is coming out empty. How could the \everypar statement be used?

Following it, is there a reliable/simple way to apply this text justification transformation to all text, instead of englobing each paragraph on something as \everypar{ My paragraph 1 text } \n\n \everypar{ My paragraph 2 text }?


For example, instead of writing:

\begin{document}

\section{Riscos}

\indent
\everypar{

    My paragraph 1, In typesetting advertisement copy, a way of justifying paragraphs has
    become popular in recent years that is somewhere between flushright
}

\medskip
\indent
\everypar{

    My paragraph 2, and raggedright setting. Lines that would stretch beyond certain limits
    are set with their glue at natural width. This single paragraph is but an
    example of this procedure; the macros are given next.
}

\end{document}

Just do something more straight forward as:

\begin{document}

\section{Riscos}

My paragraph 1, In typesetting advertisement copy, a way of justifying paragraphs has
become popular in recent years that is somewhere between flushright

\medskip
My paragraph 2, and raggedright setting. Lines that would stretch beyond certain limits
are set with their glue at natural width. This single paragraph is but an
example of this procedure; the macros are given next.

\end{document}

And still get the benefits of the smart latex text justification, offered for lines where justifying the text would not be pleasant?


Update

After @barbara-beeton comment, I think it can be done automatically using the \everypar and \par statements. Then I tried to write:

\begin{document}

\section{Riscos}

\everypar={\indent\vbox\{}
\par={\par\eatlines\}}

% \indent
% \vbox{

    In typesetting advertisement copy, a way of justifying paragraphs has
    become popular in recent years that is somewhere between flushright
    and raggedright setting. Lines that would stretch beyond certain limits
    are set with their glue at natural width. This single paragraph is but an
    example of this procedure; the macros are given next.

% \par\eatlines}

\end{document}

In hope to the statements \vbox{ and \par\eatlines\} to be inserted every paragraph start/end. However latex seems not to be accepting it, as it is throwing the error:

main2.tex:38: TeX capacity exceeded, sorry [input stack size=5000]. [    I]

main2.tex:33: Missing { inserted. [\par=]
main2.tex:33: Missing { inserted. [\par=]
main2.tex:33: Missing { inserted. [\par=]
main2.tex:33: Missing { inserted. [\par=]
main2.tex:33: Missing { inserted. [\par=]
...
Too many errors. TeX stopped.
  • in "tex by topic", you will see that the argument of \everypar is an assignment of what is to be done at the start of every paragraph. it is not the text of the paragraph. that is why your output is blank. however, since \everypar is used all over the place in latex, working around that is more complicated that i have time to tackle at the moment. – barbara beeton Apr 21 '17 at 14:40
  • After looking into it, I think it should be able to use \everypar and \par to insert automatically the statements required by the macro on every text paragraphs. Though after I tried so, it does not compiles the latex. I updated the answer within it. – user Apr 21 '17 at 15:21
  • \vbox\{ will not create a box; an "unadorned" brace is needed for that, and \{ will typeset a brace. use \bgroup and \egroup instead. (they are equivalent to the opening and closing braces.) but i'd still be very wary of messing up the existing settings of \everypar that are rife in latex. – barbara beeton Apr 21 '17 at 15:47
  • Therefore it should be \everypar={\vbox\bgroup} and \par={\eatlines\egroup}? However now it is throwing the error main2.tex:33: TeX capacity exceeded, sorry [semantic nest size=500]. [\par=]. – user Apr 21 '17 at 16:17
3

You must set locally \everypar={} in \vbox because the first letter in \vbox starts next paragraph, so next \everypar is processed. It opens a new \vbox and new \everypar is processed, etc... Never ending loop is here. This makes "TeX capacity" error.

May be, you need something like this:

\newbox\linebox \newbox\snapbox
\def\eatlines{
  \setbox\linebox\lastbox % check the last line
  \ifvoid\linebox
  \else % if it’s not empty
    \unskip\unpenalty % take whatever is
    {\eatlines} % above it;
    \setbox\snapbox\hbox{\unhcopy\linebox}
    \ifdim\wd\snapbox<.98\wd\linebox
       \box\snapbox % take the one or the other,
    \else \box\linebox \fi
  \fi}

\everypar={\setbox0=\lastbox \par 
   \vbox\bgroup \everypar={}\def\par{\endgraf\eatlines\egroup}}


    In typesetting advertisement copy, a way of justifying paragraphs has
    become popular in recent years that is somewhere between flushright
    and raggedright setting. Lines that would stretch beyond certain limits
    are set with their glue at natural width. This single paragraph is but an
    example of this procedure; the macros are given next.

    Second paragraph.

\bye

The \setbox0=\lastbox \par in \everypar removes the empty line from main vertical list (\lastbox consumes the indentation box and \par finalizes the empty paragraph: nothing is added to the main vertical list). Next material in \everypar opens \vbox and locally redefines \par for processing your \eatlines And \everypar={} is set here locally due to the reason explained above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.