问题

问题

最初我在另一个问题上解决了这个理由问题如何使用 `\everypar` 论证声明?但是现在我注意到一个新问题,我也希望能够自动解决。

通过编写此乳胶代码,我正确地得到了我想要的行为:

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

根据所用的给定因子,生成正确缩进的 PDF .98

在此处输入图片描述

但是如果行上的单词太大,则 Latex 会抛出错误Overfull \hbox (133.24454pt too wide) in paragraph at lines,输入如下:

    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.

生成输出:

在此处输入图片描述


问题

我希望 Latex 能够自动将单词raggedrightdddddddddddddddddddddddddddddddddddddddddddddddddddddddddd分成新行,而不是让它超出屏幕/文档的范围。

我知道我可以使用以下解决方案将长单词推入新行,因此该单词raggedrightdddddddddddddddddddddddddddddddddddddddddddddddddddddddddd将被拆分成一个新行:

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

生成以下文档:

在此处输入图片描述

现在,又有另一个词超出了页面限制。因此,解决方案是再次添加已经不正常的单词\lword{that}that

所以这是围绕我的 latex 文档修复进行的一项非常烦人的工作。我希望 latex 的目的是让我担心文档内容,而不是文档的美观。

我怎样才能正确制作乳胶并自动将长单词推入新行,而不是绕着文档转\lword{...}


更新 1

@大卫·卡莱尔回答我有一个新问题,使用这个文档:

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

我得到了一个不应该存在的额外洞:

在此处输入图片描述

由于长单词被推入,因此第二行上的洞是预料之中的。然而,第一行上的洞却是预料之外的。

最终的结果应该是这样的:

在此处输入图片描述

可以使用以下命令创建前一个图像\lword{raggedrightdddddddddddddddddddddddddddddddddddddddddddd},例如:

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

更新 2

问题在于文本编写的自动化。通常这种情况会由于路径而发生,但这是一个允许单词超出行的乳胶问题。因此,我想要的只是修复它。这个长单词问题的第一个乳胶工具是连字符。但我不喜欢它们。此外,我更喜欢将单词放在下一行,如本例所示:https://i.stack.imgur.com/f6Bes.png对我来说,这是一个直接的解决方案。Latex 绝不应该允许单词超出页面限制。尝试使用 Microsoft Word,它不会允许。相反,它会将单词放在下一行。

我希望行对齐,但某些包含长单词或由于对齐而过于稀疏的特定行除外。对于大多数行,对齐效果很好,但上述情况除外。

Microsoft Word 对此的处理方式如下:

在此处输入图片描述

虽然长单词前的行对齐情况很差,但不允许长单词超出行限制。

答案1

假设具体问题更像是“我需要合理地打印长文件路径”,那么这样怎么样?

在此处输入图片描述

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

相关内容