如何在每个句号后插入换行符?

如何在每个句号后插入换行符?

如何在文本中的每个句子后添加换行符?最好不要过多地更改文本,因为我必须能够在其他我不想要这种行为的情况下重复使用文本。

我尝试过在源代码中的每个句子后插入换行符,然后打开,\obeylines但这会使每个句子变成自己的段落。我只想要一个新行(类似于\\),而不是一个新段落。这让我开始寻找(我从共享中获得的)的定义,\obeylines 以便我可以定义一个替代版本,使用\\而不是\par。然而,这似乎根本没有改变它的行为。有什么提示说我做错了什么(或者是否有更好的方法)?

\documentclass[12pt]{report}

\usepackage{fullpage}

\def\myobeylines{\catcode\endlinechar\active \let \endlinechar\\ }

\newenvironment{speech}%
    {%begin
        \Large
        \setlength{\parskip}{\baselineskip}
        \setlength{\parindent}{-1cm}
        \setlength{\leftmargin}{1cm}
        \myobeylines
    }
    {%end
    }

\begin{document}

\begin{speech}
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed faucibus, sem vel suscipit eleifend, ipsum dolor tempus nunc, ut iaculis nibh arcu vitae tortor.
Pellentesque a efficitur lectus, eget sagittis lorem.
Fusce vestibulum feugiat nibh, pulvinar convallis eros sagittis ac.
Pellentesque aliquam arcu a augue malesuada, sit amet eleifend orci vulputate.
Sed nibh dolor, commodo vel risus quis, elementum consequat dui.
Pellentesque eu neque eleifend, tincidunt mauris id, ultricies odio.
Aenean ac nisi congue, mollis leo a, cursus dolor.
Aenean non justo felis.
Cras interdum quam eu metus imperdiet ultricies.
Fusce finibus pellentesque volutpat.
Praesent id lacus eget dui ullamcorper finibus nec nec urna.
Quisque vel vestibulum quam.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Mauris vel nisl eros.
\end{speech}

\end{document}

答案1

我相信只要句子以句号结尾,这就能达到你想要的效果。如果句子以任何其他句子结尾标点符号结尾,那么你需要按照我的指导对每个这样的标点符号进行操作。你的问题本质上是在问一个我很久以前问过的问题: 以不同方式解析句点以重新定义其行为的环境

我添加了一些比您要求的更多的格式,以帮助您看到更多的效果。

\documentclass{article}

\begingroup
  \catcode`\.=\active
  \gdef.{\normalperiod\formattingcommand}%
\endgroup
\newcommand\formattingcommand{\newline\makebox[0pt][r]{$\rightarrow$}\ignorespaces}
\newenvironment{speech}
  { \vspace{2ex}\par
    \let\normalperiod=.
    \catcode`\.=\active
    \sffamily\small
  }{\vspace{2ex}\par}

\begin{document}

GALLIA est omnis divisa in partes tres, quarum unam incolunt Belgae, aliam Aquitani, tertiam qui ipsorum lingua Celtae, nostra Galli appellantur. Hi omnes lingua,
institutis, legibus inter se differunt.  Gallos ab Aquitanis Garumna flumen, a Belgis Matrona et Sequana dividit.  Horum omnium fortissimi sunt Belgae, propterea quod a
cultu atque humanitate provinciae longissime absunt, minimeque ad eos mercatores saepe commeant atque ea quae ad effeminandos animos pertinent important, proximique sunt
\begin{speech}
This is the beginning of paragraph 1.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed faucibus, sem vel suscipit eleifend, ipsum dolor tempus nunc, ut iaculis nibh arcu vitae tortor.
Pellentesque a efficitur lectus, eget sagittis lorem.
Fusce vestibulum feugiat nibh, pulvinar convallis eros sagittis ac.
Pellentesque aliquam arcu a augue malesuada, sit amet eleifend orci vulputate.
Sed nibh dolor, commodo vel risus quis, elementum consequat dui.

This is the beginning of paragraph 2.
Pellentesque eu neque eleifend, tincidunt mauris id, ultricies odio.
Aenean ac nisi congue, mollis leo a, cursus dolor.
Aenean non justo felis.
Cras interdum quam eu metus imperdiet ultricies.
Fusce finibus pellentesque volutpat.
Praesent id lacus eget dui ullamcorper finibus nec nec urna.
Quisque vel vestibulum quam.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Mauris vel nisl eros.
\end{speech}
Germanis, qui trans Rhenum incolunt, quibuscum continenter bellum gerunt. Qua de causa Helvetii quoque reliquos Gallos virtute praecedunt, quod fere cotidianis proeliis
cum Germanis contendunt, cum aut suis finibus eos prohibent aut ipsi in eorum finibus bellum gerunt.

\end{document}

在此处输入图片描述

对于处理非句子结尾句号的情况,可以使用如下方法:

\chardef\periodcatcode=\catcode`\.
\newcommand\aeprotect[1]{%%
  \begingroup
  \catcode`\.=\periodcatcode
  \scantokens{#1\ignorespaces}%%
  \endgroup
  }

这是使用的结果:

在此处输入图片描述

以下是代码片段

\begin{speech}
This is the beginning of paragraph 1.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
\aeprotect{S.P.Q.R.}
That will cost you \aeprotect{\$3.25}.
This is the content INTERRUPTION \aeprotect{$3.25\cdot7.14=23.205$} following the protected periods.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
\end{speech}

如果你想做类似的事情,

\begin{speech}
  blah blah blah
  \input{file.tex}
  yada yada yada
\end{speech}

您将需要特殊处理,因为\input不了解如何.在新的 catcode 下处理。

以下代码有效

\makeatletter
\newcommand\aeinput[1]{%%
  \begingroup
  \catcode`\.=\periodcatcode
  \everyeof={\noexpand}%%
  \endlinechar=-1
  \xdef\ae@filehandle{\scantokens{#1}}%%
  \endgroup
  \input{\ae@filehandle}%%
  }%%
\makeatother

的想法everyeof{\noexpand}\endlinechar=-1 源自此处并解释如下

为什么必须用这种特定的方式来定义它,而不是\scantokens{\xdef\ae@filehandle{#1}}我希望已经回答这里

答案2

一个可能满足您目的的简单选项是使用重新定义的显式\par命令,然后将设置parskip0pt并使用\obeylines。然后将文本复制到另一个文档只需将重新定义设置\par为其原始值:

\documentclass[12pt]{report}

\usepackage{fullpage}


\newenvironment{speech}%
    {%begin
        \def\mypar{\vspace{\baselineskip}}
        \Large
        \setlength{\parskip}{0pt}
        \setlength{\parindent}{-1cm}
        \setlength{\leftmargin}{1cm}
        \obeylines
    }
    {%end
    }

\begin{document}

\begin{speech}
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed faucibus, sem vel suscipit eleifend, ipsum dolor tempus nunc, ut iaculis nibh arcu vitae tortor.
Pellentesque a efficitur lectus, eget sagittis lorem.
Fusce vestibulum feugiat nibh, pulvinar convallis eros sagittis ac.
Pellentesque aliquam arcu a augue malesuada, sit amet eleifend orci vulputate.
\mypar
Sed nibh dolor, commodo vel risus quis, elementum consequat dui.
Pellentesque eu neque eleifend, tincidunt mauris id, ultricies odio.
Aenean ac nisi congue, mollis leo a, cursus dolor.
Aenean non justo felis.
Cras interdum quam eu metus imperdiet ultricies.
Fusce finibus pellentesque volutpat.
\mypar
Praesent id lacus eget dui ullamcorper finibus nec nec urna.
Quisque vel vestibulum quam.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Mauris vel nisl eros.
\end{speech}

\end{document}

在此处输入图片描述

相关内容