将 tikzpicture 放在 \caption{} 内

将 tikzpicture 放在 \caption{} 内

我正在尝试将一个小东西放在属于环境的tikzpicture里面。这样做的原因很简单——为 PGFplot 创建标题图例,使图中和标题中的 TikZ 样式保持一致。以前我会使用破折号和包来完成此操作,但这是一个糟糕的黑客行为。在这个 MWE 中:\caption{}figurexcolor

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{figure}
\caption{\raisebox{0.5ex}{
\begin{tikzpicture}\draw[dashed,color=red,thick] (0,0) -- (0.5,0);\end{tikzpicture}}
Gas--Phase} 
\end{figure}
\end{document}

预期输出:

我的预期输出示例

生成了,但我得到了! Argument of \@caption has an extra }.! Paragraph ended before \@caption was complete.在我正在编写的文档的更大范围内,其中包括外部 .tex 文件中的章节,编译灾难性地失败(数十个错误)并且没有输出,所以我不能忽略这个例子中的警告。有什么想法吗?

答案1

PGFplots 已经内置了此功能:您可以将\labels 分配给您的图表,然后\ref在图形标题(或其他任何地方)中使用以获取图例图片:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{figure}\centering
\begin{tikzpicture}
\begin{axis}
\addplot [red, dashed, thick] {x+rand}; \label{gasphase}
\addplot [black] {x}; \label{straightline}
\end{axis}
\end{tikzpicture}
\caption{A simple diagram: \ref{gasphase} Gas--Phase;  \ref{straightline} A straight line}
\end{figure}
\end{document}

相关内容