
私はさまざまな leftskip でテキストをタイプセットしています (テキストの一部のセクションは右にインデントされています)。
何らかの理由で、リストはデフォルトで leftskip を考慮しません。ただし、興味深い xleftmargin プロパティがあります。明示的な値 (例: 2cm) を指定すると適切に機能します。
しかし、\leftskipと同じ値に設定できないようです。私はさまざまなバリエーションを試しました。
\documentclass{article}
\usepackage{listings}
\setlength{\leftskip}{5cm}
\begin{document}
totototo
\begin{lstlisting}[xleftmargin={\the\leftskip}]
def example_function():
print("Hello, world!")
\end{lstlisting}
\end{document}
しかし、役に立ちませんでした。 何を言えばいいでしょうか? MWE では を使用できます5cm
が、実際には \leftskip は常に同じではありません。
答え1
\leftskip
が動作する前にに設定される新しい長さを割り当てますlstlisting
。その後、それを の値として使用できますxleftmargin
。
\documentclass{article}
\usepackage{listings}
\newlength{\keepleftskip}
\AtBeginEnvironment{lstlisting}{\setlength{\keepleftskip}{\leftskip}}
\lstset{xleftmargin=\keepleftskip}
\begin{document}
\noindent
totototo
\begin{lstlisting}
def example_function():
print("Hello, world!")
\end{lstlisting}
\setlength{\leftskip}{5cm}
\noindent
totototo
\begin{lstlisting}
def example_function():
print("Hello, world!")
\end{lstlisting}
\setlength{\leftskip}{1cm}
\noindent
totototo
\begin{lstlisting}
def example_function():
print("Hello, world!")
\end{lstlisting}
\end{document}
答え2
Ulrike Fischer がコメントで指摘したように、\leftskip
は によってリセットされます\begin{lstlisting}
。
私ができる最も近い方法は、同じ値を持つ 2 番目の長さレジスタを作成し、それを使用することです。これは、変更するたびにそのレジスタを更新する必要があることを意味します\leftskip
。
\documentclass{article}
\usepackage{listings}
\newlength{\leftskiphack}
\begin{document}
\setlength{\leftskip}{5cm}
\setlength{\leftskiphack}{\leftskip}
totototo
\begin{lstlisting}[xleftmargin=\the\leftskiphack]
def example_function():
print("Hello, world!")
\end{lstlisting}
\setlength{\leftskip}{8cm}
\setlength{\leftskiphack}{\leftskip}
totototo
\begin{lstlisting}[xleftmargin=\the\leftskiphack]
def example_function():
print("Hello, world!")
\end{lstlisting}
\end{document}