
以下に似た(ただし同じではない)新しい環境を定義しましたlistings
:
\documentclass{ltxdoc}
\usepackage{lipsum}
\newenvironment{lines}
{\par\begingroup\nobreak
\vspace{3pt}\parindent0pt\hrule\kern5pt
\nobreak\obeylines\everypar{\strut}\endgroup}
{\par\begingroup\nobreak
\vspace{3pt}\parindent0pt\hrule\kern5pt
\nobreak\obeylines\everypar{\strut}\endgroup%
\noindent%
}
\begin{document}
\lipsum[1]
\begin{lines}
Some nifty code and stuffz
\end{lines}
\lipsum[2]
\end{document}
\noindent
テキストの前に小さなスペースを残すのはなぜですか? 何か見落としているのでしょうか?
答え1
を使用すると、\noindent
ほぼ常にこの動作が得られます。スペースは、環境の後の行末からタイプセットされる単語間スペースです。
を使用することでこの問題を回避できます\ignorespacesafterend
が、これは問題を覆い隠すだけです。 を使用するとエラーが発生\noindent
し、水平モードが早まって開始されるため、行末がスペースとしてタイプセットされます。 LaTeX 環境ではこのようなことは起こりません。 を使用して環境を定義すると、環境は垂直モードで終了しますが、空白行が続かない場合はインデントが抑制されます (を使用するとインデントが追加されないのではなく、 を使用するとtrivlist
インデントが削除されます)。\lastbox
\noindent
こんな感じで、長さはお好みで調整してください。
\documentclass{ltxdoc}
\usepackage{lipsum}
\newenvironment{linez}
{\trivlist\nopagebreak
\parindent0pt
\vspace{3pt}%
\hrule
\vspace{-3pt}%
\item\relax\obeylines}
{\par
\nopagebreak
\vspace{3pt}%
\hrule
\vspace{3pt}%
\endtrivlist}
\begin{document}
\lipsum[1]
\begin{linez}
Some nifty code and stuffz
Some nifty code and stuffz
Some nifty code and stuffz
Some nifty code and stuffz
\end{linez}
\lipsum[2]
\end{document}
答え2
が欠けています\ignorespacesafterend
が、定義では は行われません\obeylines
。 をグループで囲んでいるため、 の効果が で消えてしまうからです\endgroup
。
\documentclass{ltxdoc}
\usepackage{lipsum}
\newenvironment{linez}
{\par\nopagebreak
\vspace{3pt}
\parindent0pt
\hrule\kern5pt
\obeylines}
{\par
\nopagebreak
\vspace{3pt}
\hrule\kern5pt
\noindent\ignorespacesafterend}
\begin{document}
\lipsum[1]
\begin{linez}
Some nifty code and stuffz
Some nifty code and stuffz
Some nifty code and stuffz
Some nifty code and stuffz
\end{linez}
\lipsum[2]
\end{document}