LaTeX 文档的全局换行符

LaTeX 文档的全局换行符

我对换行有疑问。我编写了一个工具来生成包含长行(例如 URL 或路径)的报告。在我生成的 PDF 中,这些行应该换行。此外,换行应该自动完成。我知道我可以插入手动换行。但我需要一个全球的方法(长行周围没有函数),就像一条规则,或者在行结束时全局强制执行自动换行。

我已经尝试过\sloppy这个microtype包。但没有成功。

为了说明我的问题,我做了一个简单的 LaTeX 示例:

\documentclass{article}

\usepackage{microtype}
\usepackage{ragged2e}
\usepackage[hyphens]{url}
\usepackage{path}

\begin{document}

\specialpathdelimiterstrue

AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash AAAA\textbackslash 

bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

http://aaaaaaaaaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
\end{document}

所有三行都应在行/页宽度的末尾换行。

有解决方案吗?我已经搜索了好几天才找到解决方案。

答案1

我猜你不想为每一行使用一个宏,因为你的行是自动生成的。但由于你可能会使用\sloppy,所以围绕所有行的环境可能没问题。

那么一个可能的想法是扫描环境的内容并在字符之间添加自由选择的间隔。

下面的代码定义了一个环境wraplines,其中的所有行都在行尾换行。

\begin{wraplines}
% text
\end{wraplines}

环境采用可选的连字符,其实际上可以是任意受限的水平模式材料:-,,$\rightarrow$...

\begin{wraplines}[-]
% text
\end{wraplines}

代码假设环境中只出现文本。然而文本可能包含多个段落。

\documentclass{article}

\makeatletter
\newenvironment{wraplines}[1][]{%
  % encapsulate hyphenation character in a \hbox so that
  % it may be arbitrary restricted horizontal mode material e.g $\rightarrow$
  \def\@wrapdiscr{\discretionary{\hbox{#1}}{}{}}%
  \@wraplines
}{}
\newcommand\@wraplines{%
  \@ifnextchar\end\relax{%
    \@ifnextchar\par{%
      \par\expandafter\@wraplines\@gobble
    }%
    \@wraplines@aux
  }%
}
\def\@wraplines@aux#1 {\x@wraplines@aux#1\@nnil}
\newcommand*\x@wraplines@aux[1]{#1\y@wraplines@aux}
\newcommand*\y@wraplines@aux[1]{%
  \ifx#1\@nnil
    \space
    \expandafter\@wraplines
  \else
    \@wrapdiscr#1%
    \expandafter\y@wraplines@aux
  \fi
}
\makeatother

\begin{document}
\begin{wraplines}
\end{wraplines}

\noindent\verb|\begin{wraplines} ... \end{wraplines}|

\begin{wraplines}
Xxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Xxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
\end{wraplines}

\bigskip

\noindent\verb|\begin{wraplines}[-] ... \end{wraplines}|

\begin{wraplines}[-]
Xxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
\end{wraplines}

\bigskip

\noindent\verb|\begin{wraplines}[$\rightarrow$] ... \end{wraplines}|

\begin{wraplines}[$\rightarrow$]
Xxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
\end{wraplines}

\end{document}

包裹.png

相关内容