仅在特定分隔符处断行

仅在特定分隔符处断行

给出以下代码

\documentclass{article}

\newcommand\linefoo{%
\vline\hspace{0.1cm}%
}

\begin{document}

\linefoo Lorem ipsum dolor sit \linefoo amet, consectetur adipiscing elit. 
Phasellus \linefoo non varius enim. Vestibulum aliquet, \linefoo odio ut gravida
rutrum, nisl \linefoo tellus fermentum ex, eget imperdiet \linefoo velit nibh sit amet
elit. \linefoo Curabitur id semper odio, quis \linefoo interdum sapien. Vestibulum
faucibus magna \linefoo quis nulla dapibus fringilla. Donec \linefoo massa erat,
interdum vitae urna \linefoo ac, ornare vulputate felis. Proin \linefoo ut odio
eget nunc bibendum \linefoo elementum. Etiam accumsan tellus at \linefoo eros maximus
semper. Vestibulum iaculis \linefoo velit et tellus condimentum posuere. \linefoo Sed
enim lorem, tempor quis \linefoo dolor ac, volutpat volutpat elit. \linefoo Cras
tincidunt ultricies faucibus. Donec \linefoo sem lorem, ullamcorper nec volutpat
\linefoo ac, rhoncus nec turpis. Etiam \linefoo vel efficitur risus. Praesent at
\linefoo ipsum pharetra lectus convallis vehicula \linefoo quis non purus.

\end{document}

我想确保 LaTeX 会换行,这样每一行都必须以 的内容开头\linefoo(在我遇到的实际问题中, 的定义\linefoo不同)。请注意,并非每个行都\linefoo应该强制换行,因为\linefoo出现在行的中间是可以的。

我曾尝试将\linefoo其定义为

\newcommand\linefoo{%
\penalty-9999%
\vline\hspace{0.1cm}%
}

或者

\newcommand\linefoo{%
\penalty-10000%
\vline\hspace{0.1cm}%
}

但是第一个在换行时不会改变,而第二个在 之前总会换行\linefoo

我怎样才能实现这一点,最好只通过改变的定义\linefoo


以下是一个例子:给定输入

\linefoo foo bar \linefoo baz qux \linefoo foo bar \linefoo baz
qux \linefoo foo bar

LaTeX 通常会将行拆分为

| foo bar | baz qux | foo
bar | baz qux | foo bar

我希望它打破界限

| foo bar | baz qux
| foo bar | baz qux
| foo bar

而不是

| foo bar
| baz qux
| foo bar
| baz qux
| foo bar

答案1

在此处输入图片描述

\documentclass{article}

\newcommand\linefoo{%
  \ifhmode\ifinner\unskip\egroup\space\fi\fi
  \hfil\penalty0\hfilneg
  \vline\nobreak\hspace{0.1cm}%
  \hbox\bgroup\def\par{\unskip\egroup\par}%
}

\begin{document}


\linefoo Lorem ipsum dolor sit \linefoo amet, consectetur adipiscing elit. Phasellus \linefoo non varius enim. Vestibulum aliquet, \linefoo odio ut gravida rutrum, nisl \linefoo tellus fermentum ex, eget imperdiet \linefoo velit nibh sit amet elit. \linefoo Curabitur id semper odio, quis \linefoo interdum sapien. Vestibulum faucibus magna \linefoo quis nulla dapibus fringilla. Donec \linefoo massa erat, interdum vitae urna \linefoo ac, ornare vulputate felis. Proin \linefoo ut odio eget nunc bibendum \linefoo elementum. Etiam accumsan tellus at \linefoo eros maximus semper. Vestibulum iaculis \linefoo velit et tellus condimentum posuere. \linefoo Sed enim lorem, tempor quis \linefoo dolor ac, volutpat volutpat elit. \linefoo Cras tincidunt ultricies faucibus. Donec \linefoo sem lorem, ullamcorper nec volutpat \linefoo ac, rhoncus nec turpis. Etiam \linefoo vel efficitur risus. Praesent at \linefoo ipsum pharetra lectus convallis vehicula \linefoo quis non purus.

\end{document}

答案2

一些低级诡计:

\documentclass{article}

\newcommand\linefoo{\leavevmode\linebreak[0]\vline}

\newenvironment{foo}
 {\endlinechar=32 \obeyspaces
  \begingroup\lccode`~=32 \lowercase{\endgroup\let~}\nobreakspace
  \hyphenpenalty10000
  \raggedright\par
 }
 {\par}

\begin{document}

\begin{foo}
\linefoo Lorem ipsum dolor sit \linefoo amet, consectetur adipiscing elit. 
Phasellus \linefoo non varius enim. Vestibulum aliquet, \linefoo odio ut gravida
rutrum, nisl \linefoo tellus fermentum ex, eget imperdiet \linefoo velit nibh sit amet
elit. \linefoo Curabitur id semper odio, quis \linefoo interdum sapien. Vestibulum
faucibus magna \linefoo quis nulla dapibus fringilla. Donec \linefoo massa erat,
interdum vitae urna \linefoo ac, ornare vulputate felis. Proin \linefoo ut odio
eget nunc bibendum \linefoo elementum. Etiam accumsan tellus at \linefoo eros maximus
semper. Vestibulum iaculis \linefoo velit et tellus condimentum posuere. \linefoo Sed
enim lorem, tempor quis \linefoo dolor ac, volutpat volutpat elit. \linefoo Cras
tincidunt ultricies faucibus. Donec \linefoo sem lorem, ullamcorper nec volutpat
\linefoo ac, rhoncus nec turpis. Etiam \linefoo vel efficitur risus. Praesent at
\linefoo ipsum pharetra lectus convallis vehicula \linefoo quis non purus.
\end{foo}

\end{document}

在此处输入图片描述

答案3

\documentclass{article} 
\newcommand\linefoo{% 
\ifhmode\ifinner\unskip\egroup\space\fi\fi \hfil\penalty0\hfilneg 
\vline\nobreak\hspace{0.1cm}% 
\hbox\bgroup\def\par{\unskip\egroup\par}% 
 } 
\begin{document} 
\linefoo Lorem ipsum dolor sit 
\linefoo amet, consectetur adipiscing elit. Phasellus 
\linefoo non varius enim. Vestibulum aliquet, 
\linefoo odio ut gravida rutrum, nisl 
\linefoo tellus fermentum ex, eget imperdiet 
\linefoo velit nibh sit amet elit. 
\linefoo Curabitur id semper odio, quis 
\linefoo interdum sapien. Vestibulum faucibus magna 
\linefoo quis nulla dapibus fringilla. Donec 
\linefoo massa erat, interdum vitae urna 
\linefoo ac, ornare vulputate felis. Proin 
\linefoo ut odio eget nunc bibendum 
\linefoo elementum. Etiam accumsan tellus at
\linefoo eros maximus semper. Vestibulum iaculis 
\linefoo velit et tellus condimentum posuere. 
\linefoo Sed enim lorem, tempor quis 
\linefoo dolor ac, volutpat volutpat elit. 
\linefoo Cras tincidunt ultricies faucibus. Donec 
\linefoo sem lorem, ullamcorper nec volutpat 
\linefoo ac, rhoncus nec turpis. Etiam 
\linefoo vel efficitur risus. Praesent at 
\linefoo ipsum pharetra lectus convallis vehicula 
\linefoo quis non purus. 
\end{document}

在此处输入链接描述

1:http://enter code here 在此处输入图片描述 \documentclass{article} \newcommand\linefoo{% \ifhmode\ifinner\unskip\egroup\space\fi\fi \hfil\penalty0\hfilneg \vline\nobreak\hspace{0.1cm}% \hbox\bgroup\def\par {\unskip\egroup\par}% } \begin{document} \linefoo Lorem ipsum dolor sit \linefoo amet, consectetur adipiscing elit。相思树 \linefoo non varius enim。 Vestibulum aliquet,\linefoo odio ut gravida rutrum,nisl \linefoo tellus fermentum ex,eget imperdiet \linefoo velit nibh sit amet elit。 \linefoo Curabitur id semper odio,quis \linefoo interdum sapien。 Vestibulum faucibus magna \linefoo quis nulla dapibus fringilla。 Donec \linefoo massa erat,interdum vitae urna \linefoo ac,ornare vulputate felis。 Proin \linefoo ut odio eget nunc bibendum \linefoo elementum。那些总是在\linefoo eros maximus semper 中讲述的人。 Vestibulum iaculis \线粒体被保护起来并处于兴奋状态。 \linefoo Sed enim lorem,tempor quis \linefoo dolor ac,volutpat volutpat elit。 \linefoo Cras tincidunt ultricies faucibus。 Donec \linefoo sem lorem,ullamcorper nec volutpat \linefoo ac,rhoncus nec turpis。 Etiam \linefoo vel efficitur risus。出席于\linefoo ipsum pharetra lectus convallis vehicula \linefoo quis non purus。 \结束{文档}

enter code 
  • 项目清单

相关内容