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.

  • 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
  • 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
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}
  • 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: 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: 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

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.