长行文本的自动换行?

长行文本的自动换行?

这里只是举个例子,我有一行:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa

现在,我想让 LaTeX 根据文本块/边距的宽度自动换行。

答案1

你可能正在寻找类似序列分割包裹。

\documentclass[11pt]{article}
\usepackage{seqsplit}

\begin{document}
  \seqsplit{aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa}
\end{document}

更新:

对于有空格的行,该\seqsplit命令当然可以使用多次。

\documentclass[11pt]{article}
\usepackage{seqsplit}

\begin{document}
  \seqsplit{aaaaaaaaaaaaaaaaaaaaaaaa} \seqsplit{aaaaaaaaaaaaaaaaaaaaaaaaaaaaa} \seqsplit{aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa} \seqsplit{aaaaaaaaaaaaaaa}
\end{document}

答案2

如果单词太长,并且没有连字符模式,TeX 引擎就不知道在哪里插入断字。您可以通过在字母之间添加少量的粘连来强制断字。然后 TeX 就可以插入断字。需要多少粘连?实际上,即使是最小单位 1sp 也可以达到目的(一个点中有 65 536 个缩放点,小于可见光的波长)。我们只需要一个扫描仪来扫描字母。下面是一个最小单位:

\documentclass{article}
\usepackage{lipsum}
\begin{document}
\parindent0pt
\makeatletter
\def\scanfunction#1{#1}
\let\tempa\@empty
\def\scan@letters#1#2{%
   \g@addto@macro{\tempa}{#1\hskip 0pt plus 1sp minus 1sp}%
   \ifx#2\@empty
     \else 
       \expandafter\scan@letters
   \fi
#2}

\def\scan#1{%
  \scan@letters #1\@empty
}
\scan{aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa}
\tempa

\lipsum[1]
\end{document}

编辑:聊天中的 egreg 引起了我的注意,甚至hskip 0pt可以起作用。

答案3

您可以调整解决方案以使用回车符来中断 URL 的选项?使用了连字符包在每个字符后添加一个可破坏字符。

以下是不同宽度的输出。最后一段的宽度经过选择,以确保如果在空格处发生中断,则不会添加连字符。

在此处输入图片描述

\documentclass[border=5pt]{standalone}
\usepackage{hyphenat}
\usepackage{xstring}
\usepackage{forloop}

\newsavebox\MyBreakChar%
\sbox\MyBreakChar{\hyp}% char to display the break after non char
\newsavebox\MySpaceBreakChar%
\sbox\MySpaceBreakChar{}% char to display the break after space
\makeatletter%
\newcommand*{\BreakableChar}[1][\MyBreakChar]{%
  \leavevmode%
  \prw@zbreak%
  \discretionary{\usebox#1}{}{}%
  \prw@zbreak%
}%

\newcounter{index}%
\newcommand{\AddBreakableChars}[1]{%
  \StrLen{#1 }[\stringLength]%
  \forloop[1]{index}{1}{\value{index}<\stringLength}{%
    \StrChar{#1}{\value{index}}[\currentLetter]%
    \IfStrEq{\currentLetter}{ }
        {\currentLetter\BreakableChar[\MySpaceBreakChar]}%
        {\currentLetter\BreakableChar[\MyBreakChar]}%
  }%
}%

\newcommand*{\MyLongString}{aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa caaaaaaaaaaaaaaaaaa}%

\begin{document}
\parbox{4cm}{\AddBreakableChars{\MyLongString}}

\bigskip
\parbox{7cm}{\AddBreakableChars{\MyLongString}}

\bigskip
\parbox{9.1cm}{\AddBreakableChars{\MyLongString}}
\end{document}

答案4

如果您使用 Maple teX 导出作为文件源,则只需将 {2d} 全局更改为 {3d}。然后行将换行。

例子:

这不会换行:

\mapleinline{inert}{2d}{[animate, animate3d, animatecurve, arrow, changecoords, complexplot, complexplot3d, conformal, conformal3d, contourplot, contourplot3d, coordplot, coordplot3d, densityplot, display, dualaxisplot, fieldplot, fieldplot3d, gradplot, gradplot3d, implicitplot, implicitplot3d, inequal, interactive, interactiveparams, intersectplot, listcontplot, listcontplot3d, listdensityplot, listplot, listplot3d, loglogplot, logplot, matrixplot, multiple, odeplot, pareto, plotcompare, pointplot, pointplot3d, polarplot, polygonplot, polygonplot3d, polyhedra_supported, polyhedraplot, rootlocus, semilogplot, setcolors, setoptions, setoptions3d, spacecurve, sparsematrixplot, surfdata, textplot, textplot3d, tubeplot]}

但这将(更改2d3d):

\mapleinline{inert}{3d}{[animate, animate3d, animatecurve, arrow, changecoords, complexplot, complexplot3d, conformal, conformal3d, contourplot, contourplot3d, coordplot, coordplot3d, densityplot, display, dualaxisplot, fieldplot, fieldplot3d, gradplot, gradplot3d, implicitplot, implicitplot3d, inequal, interactive, interactiveparams, intersectplot, listcontplot, listcontplot3d, listdensityplot, listplot, listplot3d, loglogplot, logplot, matrixplot, multiple, odeplot, pareto, plotcompare, pointplot, pointplot3d, polarplot, polygonplot, polygonplot3d, polyhedra_supported, polyhedraplot, rootlocus, semilogplot, setcolors, setoptions, setoptions3d, spacecurve, sparsematrixplot, surfdata, textplot, textplot3d, tubeplot]}

相关内容