PGFP圖和清單不相容?

PGFP圖和清單不相容?

我使用 ` 作為我的短內聯列表分隔符號。這編譯得很好:

\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

問題是清單不想停用已經停用的捷徑。當 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}

相關內容