我想在每个换行符中添加一些内容,如下所示:
\documentclass{article}
\newenvironment{mystuff}{dark magic goes here}{or here}
\begin{document}
\begin{mystuff}
first row
second row
\end{mystuff}
\end{document}
我想要 Latex 将它渲染成这样:
first row -and this was added by mystuff environment
second row -and this was added by mystuff environment
我该怎么做?
答案1
正如 Henri Menke 所说,这个^^M
技巧有风险,但在紧急情况下确实有用。我编辑了它以使用\obeylines
(本质上是等效的和有风险的),这可以稍微简化代码。我还删除了\makeatletter
...\makeatother
命令。我稍微修改了代码,以便您可以即时指定要添加的文本:
\documentclass{article}
\pagestyle{empty}% for cropping
\usepackage{blindtext}
%\def\specialccr{\mytext\newline}
\def\specialccr{\mytext\par}
{\obeylines
\gdef\specialcr{\obeylines \let^^M=\specialccr}%
}
\newenvironment{mystuff}[1]{%
\def\mytext{#1}%
\parindent0pt
\leftskip3em
\everypar={$\bullet$\ }
\par
\noindent
\specialcr}{%
}
\begin{document}
\blindtext[1]
\begin{mystuff}{\ -- and this was added by mystuff environment}% <- % Necessar
first row
second row
third row
\end{mystuff}
\blindtext[1]
\end{document}
最后再编辑一下。原来使用的\newline
which 不会开始新的段落。我替换了\par
which ,从而允许各种有趣的格式:
答案2
我正在使用活动的行尾字符^^M
。这是不是这不是一个好的解决方案,但它确实有效。
\documentclass{article}
\pagestyle{empty}% for cropping
\usepackage{blindtext}
\makeatletter
\def\special@cr{!\newline}
{\catcode`\^^M=\active%
\gdef\specialcr{\catcode`\^^M=\active \let ^^M=\special@cr}%
}
\makeatother
\newenvironment{mystuff}{\par\noindent\specialcr}{}
\begin{document}
\blindtext[1]
\begin{mystuff}% You will want to suppress the ^^M here.
first row
second row
\end{mystuff}
\blindtext[1]
\end{document}