PGFPlots と Listings に互換性がない?

PGFPlots と Listings に互換性がない?

私は短いインライン リスト区切り文字として ` を使用しています。これは正常にコンパイルされます:

\documentclass{article}
\usepackage{listings, pgfplots}
\pgfplotsset{compat=1.14}
\tikzset{every picture/.style={execute at begin picture={\lstDeleteShortInline{`}}, execute at end picture={\lstMakeShortInline{`}}}}
\AtBeginDocument{\lstMakeShortInline{`}}
\begin{document}
\begin{tikzpicture}
\draw node{Hello};
\end{tikzpicture}
\end{document}

しかし、これはエラーをスローします:

\documentclass{article}
\usepackage{listings, pgfplots}
\pgfplotsset{compat=1.14}
\tikzset{every picture/.style={execute at begin picture={\lstDeleteShortInline{`}}, execute at end picture={\lstMakeShortInline{`}}}}
\AtBeginDocument{\lstMakeShortInline{`}}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot coordinates {(1,1) (2,2)};
\end{axis}
\end{tikzpicture}
\end{document}

エラーは次のとおりです:

Package Listings Error: ` is not a short reference for \lstinline.

ドキュメント内で pgfplots とバックティックで区切られたインライン コード スニペットの両方を使用する方法はありますか?

答え1

問題は、listings がすでに非アクティブになっているショートカットを非アクティブにしたくないことです。また、pgfplots が画像をネストすると、エラーが発生します。二重設定を回避する独自の非アクティブ化マクロを作成できます。

\documentclass{article}
\usepackage{listings}
\usepackage{pgfplots}
\newcommand\deactivemyshortinline{%
 \expandafter\ifx\csname lst@ShortInlineOldCatcode\string`\endcsname\relax
  \else  
   \lstDeleteShortInline{`}%
 \fi}

\pgfplotsset{compat=1.14}
\tikzset{every picture/.style={execute at begin picture={\deactivemyshortinline}}, execute at end picture={\lstMakeShortInline{`}}}
\AtBeginDocument{\lstMakeShortInline{`}}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot coordinates {(1,1) (2,2)};
\end{axis}
\end{tikzpicture}
\end{document}

関連情報