1

Initially I solved this justification problem on this other question How could the `\everypar` justification statement be used?, however now I am noticing a new problem which I also would like to be done automatically.

By writing this latex code I correctly got my desired behavior:

% 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}

\begin{document}

\section{Riscos}

\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 raggedrightddddd 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.

    \medskip
    Second paragraph.

\end{document}

Which produces this PDF, correctly indented, given .98 factor used:

enter image description here

However if there is a word too big on the line, latex is throwing the error Overfull \hbox (133.24454pt too wide) in paragraph at lines, given the input:

    In typesetting advertisement copy, a way of justifying paragraphs has
    become popular in recent years that is somewhere between flushright
    and raggedrightdddddddddddddddddddddddddddddddddddddddddddddddddddddddddd 
    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.

    \medskip
    Second paragraph.

Which generates the output:

enter image description here


Question

I would like latex to automatically break the word raggedrightdddddddddddddddddddddddddddddddddddddddddddddddddddddddddd into a new line, instead of allowing it to go beyond of the screen/document.

I know I can use the following solution from Push long words in a new line, so the word raggedrightdddddddddddddddddddddddddddddddddddddddddddddddddddddddddd will be break into a new line:

% Allow to push long words on new lines when they do not fit entirely on the current line.
\newcommand\lword[1]{\leavevmode\nobreak\hskip0pt plus\linewidth\penalty50\hskip0pt plus-\linewidth\nobreak{#1}}

    In typesetting advertisement copy, a way of justifying paragraphs has
    become popular in recent years that is somewhere between flushright
    and \lword{raggedrightdddddddddddddddddddddddddddddddddddddddddddddddddddddddddd}
    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.

    \medskip
    Second paragraph.

Which generates the following document:

enter image description here

Which now, also has another word breaking the page limits. Therefore the solution would be again to put \lword{that} on the that word which had been gone rogue.

So this is a very annoying work to go around my latex document fixing. I hope the latex intend is to allow me worry about the document contents, instead of the documents aesthetics.

How can I make latex correctly and automatically push long words into new lines, instead of going around the document putting \lword{...}?


Update 1

After @David Carlisle answer I have a new problem, using this document:

% 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}

\begin{document}

\section{Riscos}

\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}}

\noindent X\dotfill X

\sloppy
\rightskip 0pt plus 1pt
\emergencystretch\textwidth
\hyphenpenalty=10000
\parskip\baselineskip

    In typesetting advertisement copy, a way of justifying paragraphs has
    become popular in recent years that is somewhere between flushright
    and raggedrightdddddddddddddddddddddddddddddddddddddddddddd 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.

    \medskip
    Second paragraph.

\end{document}

I got an extra hole, which should not exists:

enter image description here

The hole on the second line is expected as the long word was pushed. However the hole on the first line is not expected.

The final should be like this:

enter image description here

This former image can be created using the command \lword{raggedrightdddddddddddddddddddddddddddddddddddddddddddd}, example:

% 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}

\begin{document}

\section{Riscos}

\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}}

\newcommand\lword[1]{\leavevmode\nobreak\hskip0pt plus\linewidth\penalty50\hskip0pt plus-\linewidth\nobreak{#1}}

    In typesetting advertisement copy, a way of justifying paragraphs has
    become popular in recent years that is somewhere between flushright
    and \lword{raggedrightdddddddddddddddddddddddddddddddddddddddddddd} 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.

    \medskip
    Second paragraph.

\end{document}

Update 2

The problem is automation of the text writing. Usually this happens due paths, however it is a latex problem allowing words to go beyond the line. Therefore, what I want to is just a fix for it. The first latex tool for this long word problem are hyphens. However I do not like them. Moreover I prefer just to place the word to the next line, as on this example: https://i.stack.imgur.com/f6Bes.png, For me, it is a straight forward solution. Latex should never allow a word to go beyond the page limit. Try to do it Microsoft Word, it will not allow. Instead, it will put the word to the next line.

I would like line justification, except for some specific lines which contains long words or are too much sparse due the justification. For the most lines, justification works fine, except the mentioned cases.

This is how Microsoft Word deals with it:

enter image description here

While the line before the long word was badly justified, it do not allowed the long word to go beyond the line limit.

user
  • 4,745
  • Could you give an example of such long word? – Sigur Apr 22 '17 at 20:01
  • 4
    what is the actual use case here? you have disabled almost all of tex's hyphenation and justification and so are having to do more and m ore "by hand" – David Carlisle Apr 22 '17 at 20:02
  • that isn't a word it is four words separated by / and . if the real question is to allow breaking on such characters that is a completely different question!!!! – David Carlisle Apr 22 '17 at 20:03
  • 1
    If you posted a real paragraph that you want to set with no over full boxes, we could probably show how to do that just by setting the paragraph settings without any of this unnatural boxing – David Carlisle Apr 22 '17 at 20:04
  • So, how to make latex break my paragraphs if it found big words? The example I posted is just an extreme case for easy reproduction. – user Apr 22 '17 at 20:07
  • 4
    if the problem is several words strung together, separated by slashes, you might try using \slash instead of a typed-in slash. it allows a break after the slash, without a following hyphen. (and the use case you describe in comments would better be added to the text of the question.) – barbara beeton Apr 22 '17 at 20:25
  • I just noticed I misused the word break/broke, while I actually meant push the long word into a new line. – user Apr 22 '17 at 23:07
  • Just to be very very sure, you do know that raggedrightdddddddddddddddddddddddddddddddddddddd isn't a word known to the english language? I am not aware of any human language. You cannot hyphenate something that isn't a word. – Johannes_B Apr 23 '17 at 04:49
  • That is kind the point to push them to a new line. The ideal would latex to push to a new line, what it cannot hyphenate, and after, if it still not fitting on a new line by itself, then do what most WYSIWYG rich text editor as Microsoft Word does, break then to another new line without hyphenate them. Example: http://i.imgur.com/RxlOkyf.png – user Apr 23 '17 at 05:20

1 Answers1

2

Assuming the specific question is more like "I need to sensibly print long file paths", how about this?

enter image description here

\documentclass[12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[a4paper, margin=2cm]{geometry}

% https://tex.stackexchange.com/a/10401
\usepackage[hyphens]{url}
\expandafter\def\expandafter\UrlBreaks\expandafter{\UrlBreaks%  save the current one
  \do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j%
  \do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t%
  \do\u\do\v\do\w\do\x\do\y\do\z\do\A\do\B\do\C\do\D%
  \do\E\do\F\do\G\do\H\do\I\do\J\do\K\do\L\do\M\do\N%
  \do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V\do\W\do\X%
  \do\Y\do\Z}

\begin{document}

\section{Riscos}

Original bad breaks, Assim após ativar os testes, basta abrir o arquivo
ArtificialIntelligence/AnswersGuessAiGame/players.py, encontrar a classe ...
Tests logo do final do arquivo e escolher quais dos testes se quer executar.

Option 1, rewrite the paragraph to avoid bad breaks.
Assim após ativar os testes, basta abrir o arquivo
ArtificialIntelligence/AnswersGuessAiGame/players.py, encontrar a classe ...
Tests logo do final do arquivo e escolher quais dos testes se quer executar.

Option 2, use the \verb|\path| command from the \verb|url| package, and allow
URLs to break after any character.
Assim após ativar os testes, basta abrir o arquivo
\path{ArtificialIntelligence/AnswersGuessAiGame/players.py}, encontrar a classe ...
Tests logo do final do arquivo e escolher quais dos testes se quer executar.

\end{document}
Mike Renfro
  • 20,550
  • The problem is automation of the text writing. Usually this happens due paths, however it is a latex problem allowing words to go beyond the line. Therefore, what I want to is just a fix for it. The first latex tool for this long word problem are hyphens. However I do not like them. Moreover I prefer just to place the word to the next line, as on this example: https://i.stack.imgur.com/f6Bes.png, For me, it is a straight forward solution. Latex should never allow a word to go beyond the page limit. Try to do it Microsoft Word, it will not allow. Instead, it will put the word no the next line. – user Apr 22 '17 at 22:44
  • However, you \path command is a nice thing to use for writing paths specifically. – user Apr 22 '17 at 22:51
  • 1
    The mechanism that allows the path command can allow for any arbitrary text to be broken across lines(variables, class names, function names). Hyphenation is standard typography for justified text. If you don't want the text justified, I suppose you can avoid it, but you'll get a higher rate of awkward line breaks and very ragged paragraphs. – Mike Renfro Apr 22 '17 at 22:54
  • I would like line justification, except for some specific lines which contains long words or are too much sparse due the justification. For the most lines, justification works fine, except the mentioned cases, like on this example: https://i.stack.imgur.com/f6Bes.png – user Apr 22 '17 at 22:58
  • Justified text requires adjusting the spaces between words. If that's the only tool at your disposal (no hyphenation, no breaking words like with the \path command), you'll get those incredibly spread-out five-word lines like you show from Word. What you appear to be asking for is a paragraph that's "mostly" justified, and the amount of effort you're going through could be likened to the symptoms of a code smell. – Mike Renfro Apr 22 '17 at 23:10
  • I think most typographers would go with full justification, including hyphenation for normal words of the given language, and breaking up your extra-long "fake" words for variable and other names in your source code. I don't know what other use cases you'd have in your automation application, since you've not provided any. – Mike Renfro Apr 22 '17 at 23:12
  • Thanks @Mike Renfro, you put it right a paragraph that's "mostly" justified. However in about 99% of the cases the paragraphs are going to be fully justified, due the correct latex hyphenation and justification. Moreover, this is my last use case. The first one I solved with https://tex.stackexchange.com/questions/365818/how-could-the-everypar-justification-statement-be-used, therefore only remains this particular case for solving. When this is finished, the latex use cases for justification are complete. – user Apr 23 '17 at 00:15
  • Currently I can solve this problem manually by using \lword{} when I got the bad boxes warning, however this case could be done automatically if latex push the long words to new lines, instead of allowing them to go beyond the page limit. – user Apr 23 '17 at 00:15