lstlisting: 強調表示されたコードを揃える方法

lstlisting: 強調表示されたコードを揃える方法

コードの一部を強調表示するために、いくつかの新しいコマンドを定義したいと思います (これは実際にはプログラミング言語用ではなく、トレース ダンプ用です)。

私の問題は、色付きのテキストをリストのテキストと適切に揃えることができないようです。「Hello worlds」を参照してください。

新しいコマンド定義を追加してみました\ttfamilyが、効果はありませんでした。また、さまざまなフォント サイズを試してみましたが、色付きテキスト フォントと色なしテキスト フォントのサイズは同じなので、それが問題ではないと思います。

\documentclass{article}

\usepackage{listings}
\lstset{
  basicstyle=\ttfamily\large,
  breakatwhitespace=false,
  breaklines=true,
  escapeinside={\%*}{*},
}

\usepackage{color}
\definecolor{mycolorGreenOne}{rgb}{0, 0.7, 0}
\newcommand*{\listingsHigh}[1]{\textcolor{red}{#1}}
\newcommand*{\LighthighOne}{\color{cyan}}

\begin{document}
\begin{lstlisting}
My text: %*\LighthighOne Hello world!* Goodbye!
My text: Hello world! Goodbye!
My text: %*\listingsHigh{Hello world!}* Goodbye!
\end{lstlisting}
\end{document}

答え1

問題は解決しました! 以下を追加しました: columns=fullflexible

ドキュメントの fullflexible の説明は、実際に行われていることと比べると、直感に反しているように思われます。これは、fixed は列を「整列」させ、flexible は列を「破棄」して、元のフォントの整列が尊重されるようにすることを示しているように見えるからです。ただし、fixedは機能しませんが、fullflexible機能します。誰かがその理由を説明してくれるとありがたいです。

セクション2.10を参照してください。リスト pdf

\documentclass{article}

\usepackage{listings}
\lstset{
  basicstyle=\ttfamily\large,
  breakatwhitespace=false,
  breaklines=true,
  escapeinside={\%*}{*},
  columns=fullflexible
}

\usepackage{color}
\definecolor{mycolorGreenOne}{rgb}{0, 0.7, 0}
\newcommand*{\listingsHigh}[1]{\textcolor{red}{#1}}
\newcommand*{\LighthighOne}{\color{cyan}}

\begin{document}
\begin{lstlisting}
My text: %*\LighthighOne Hello world!* Goodbye!
My text: Hello world! Goodbye!
My text: %*\listingsHigh{Hello world!}* Goodbye!
\end{lstlisting}
\end{document}

関連情報