環境内のカスタム改行スタイル

環境内のカスタム改行スタイル

次のように、各改行に何かを追加したいと思います。

\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}

最後にもう 1 つ編集します。元のコードで\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}

ここに画像の説明を入力してください

関連情報