我想在每個換行符中添加一些內容,如下所示:
\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
,不開始新段落。我替換了\par
它,從而允許各種有趣的格式:
答案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}