我想将 PGF 绘图标记添加到 Tikz 图形的标题中。我定义了绘制这些标记的命令,例如
\def\showpgfcircle{\tikz[baseline=-0.9ex]\node[blue,mark size=0.7ex]{\pgfuseplotmark{o}};}
在文本中效果很好(虽然我在标记后遇到了间距问题),但在标题中却无法编译。您有什么建议吗?
我的 MWE 是:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{smithchart}
\usetikzlibrary{
arrows,
positioning,
calc,
circuits.ee.IEC,
external,
plotmarks}
\usetikzlibrary{pgfplots.units}
\pgfplotsset{ every axis label/.append style={font=\scriptsize}, every tick label/.append style={font=\scriptsize,text depth=.25ex}}
%set labels in all diagrams in scriptsize
\pgfkeys{/pgf/number format/.cd, use comma, set thousands separator={ }}
% use comma for dezimal numbers; no use of thoudends seperator
\def\showpgfcircle{\tikz[baseline=-0.9ex]\node[blue,mark size=0.7ex]{\pgfuseplotmark{o}};}
\begin{document}
\begin{figure}[t] % Example plot with markers
\centering
\begin{tikzpicture}
\begin{smithchart}[scale=1]
\pgfsetplotmarksize{0.7ex}
\addplot[draw=blue,mark=o, only marks, is smithchart cs] coordinates {
(0.041457198, -0.56082138) (-0.46495696, -0.31631368) (-0.50641416, 0.2445077) (-0.041457198, 0.56082138) (0.46495696, 0.31631368) (0.50641416, -0.2445077) (-0.42592593, 0.73772534) (0.42592593, 0.73772534) (0.42592593, -0.73772534) (-0.42592593, -0.73772534)
};
\addplot[draw=red,mark=x, only marks, is smithchart cs] coordinates {
(0.041457198, -0.56082138) (-0.46495696, -0.31631368) (-0.50641416, 0.2445077) (-0.041457198, 0.56082138) (0.46495696, 0.31631368) (0.50641416, -0.2445077) (-0.42592593, 0.73772534) (0.42592593, 0.73772534) (0.42592593, -0.73772534) (-0.42592593, -0.73772534)
};
\end{smithchart}
\end{tikzpicture}
\caption{Example caption. I want to add the marker here.}
\end{figure}
Example text with PGFmarker like \showpgfcircle included. This works, but there is no space after the marker.
\end{document}
答案1
您的代码几乎正确。仅缺少两点。
首先,您提到的空格不足是因为 TeX 会吃掉宏名后的空格(请参阅 TeXBook 的第 3 章,它很短,解释了很多)。您必须使用\⍽
(⍽
表示正常空格) 强制宏名后的空格。
补充说明:xspace
包,它执行各种检查并尝试确定是否应该在宏名称后插入空格,因此您不必担心\⍽
在宏名称后输入。但在使用它之前,请参阅xspace 的缺点。
如果您选择使用xspace
,则您的宏将变为:
\def\showpgfcircle{\tikz[baseline=-0.9ex]\node[blue,mark size=0.7ex]{\pgfuseplotmark{o}};\xspace}
并将其用于:
The macro \showpgfcircle will insert a space in the first case, but not here: \showpgfcircle.
其次,要将 添加tikzpicture
到标题中,您必须\protect
这样做,因为图形标题已写入 .lof 文件以创建图形列表。请参阅\protect 的用途是什么?和脆弱命令和坚固命令之间有什么区别?。
这是您的代码:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{smithchart}
\usetikzlibrary{
arrows,
positioning,
calc,
circuits.ee.IEC,
external,
plotmarks}
\usetikzlibrary{pgfplots.units}
\pgfplotsset{ every axis label/.append style={font=\scriptsize}, every tick label/.append style={font=\scriptsize,text depth=.25ex}}
%set labels in all diagrams in scriptsize
\pgfkeys{/pgf/number format/.cd, use comma, set thousands separator={ }}
% use comma for dezimal numbers; no use of thoudends seperator
\def\showpgfcircle{\tikz[baseline=-0.9ex]\node[blue,mark size=0.7ex]{\pgfuseplotmark{o}};}
\begin{document}
\begin{figure}[t] % Example plot with markers
\centering
\begin{tikzpicture}
\begin{smithchart}[scale=1]
\pgfsetplotmarksize{0.7ex}
\addplot[draw=blue,mark=o, only marks, is smithchart cs] coordinates {
(0.041457198, -0.56082138) (-0.46495696, -0.31631368) (-0.50641416, 0.2445077) (-0.041457198, 0.56082138) (0.46495696, 0.31631368) (0.50641416, -0.2445077) (-0.42592593, 0.73772534) (0.42592593, 0.73772534) (0.42592593, -0.73772534) (-0.42592593, -0.73772534)
};
\addplot[draw=red,mark=x, only marks, is smithchart cs] coordinates {
(0.041457198, -0.56082138) (-0.46495696, -0.31631368) (-0.50641416, 0.2445077) (-0.041457198, 0.56082138) (0.46495696, 0.31631368) (0.50641416, -0.2445077) (-0.42592593, 0.73772534) (0.42592593, 0.73772534) (0.42592593, -0.73772534) (-0.42592593, -0.73772534)
};
\end{smithchart}
\end{tikzpicture}
\caption{Example caption. I want to add the marker here: \protect\showpgfcircle\ see? }
\end{figure}
Example text with PGFmarker like \showpgfcircle\ included. This works, but there is no space after the marker.
\end{document}