我的代码如下,我想在 lstlisting 环境中引用 Tikz 图像,而不是按原样显示它。这种情况经常出现在代码注释中。
\documentclass{article}
\usepackage{listings}
\usepackage{tikz}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\draw (0,0) circle [radius=1cm];
\node at (0,0) { Center};
\end{tikzpicture}
\caption{tikzfigure}
\label{circ:center}
\end{figure}
This text refers to the label in the previous Figure \ref{circ:center}.
\begin{lstlisting}
//This text refers to the label in the previous Figure \ref{circ:center}.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
\end{lstlisting}
\end{document}
答案1
您需要逃避参考:
\documentclass{article}
\usepackage{listings}
\usepackage{tikz}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\draw (0,0) circle [radius=1cm];
\node at (0,0) { Center};
\end{tikzpicture}
\caption{tikzfigure}
\label{circ:center}
\end{figure}
This text refers to the label in the previous Figure \ref{circ:center}.
\begin{lstlisting}[escapeinside=`']
//This text refers to the label in the previous Figure `\ref{circ:center}'.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
\end{lstlisting}
\end{document}