我使用 ` 作为短内联列表分隔符。这可以很好地编译:
\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}