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:
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:
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:
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:
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:
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:
While the line before the long word was badly justified, it do not allowed the long word to go beyond the line limit.
Best Answer
Assuming the specific question is more like "I need to sensibly print long file paths", how about this?