![使用 \NewDocumentCommand 控制 TikZ 节点中的标签文本:包括或排除描述](https://linux22.com/image/473774/%E4%BD%BF%E7%94%A8%20%5CNewDocumentCommand%20%E6%8E%A7%E5%88%B6%20TikZ%20%E8%8A%82%E7%82%B9%E4%B8%AD%E7%9A%84%E6%A0%87%E7%AD%BE%E6%96%87%E6%9C%AC%EF%BC%9A%E5%8C%85%E6%8B%AC%E6%88%96%E6%8E%92%E9%99%A4%E6%8F%8F%E8%BF%B0.png)
我正在处理一个文档,其中我使用 TikZ 节点包含图像。我有一个命令\myincludegraphics
,其中包含一个图像以及一个矩形和一个标签节点。但是,我很难控制标签节点的文本。具体来说,我希望在使用该命令时可以选择包含或排除标签文本。我该如何实现此功能?任何帮助或指导都将不胜感激。谢谢!
请注意,我需要以下可能性:
图像没有 rectangle
-节点和没有 label
-节点:
\myincludegraphics*{example-image}
图像使用默认 rectangle
-节点和没有 label
-节点:
\myincludegraphics{example-image}
图像具体 rectangle
-节点和没有 label
-节点:
\myincludegraphics[0.25,0.35][0.75,0.65]{example-image}
图像没有 rectangle
-节点和使用默认 label
-节点:
\myincludegraphics[ShowDefaultLabel]{example-image}
图像具体 rectangle
-节点和使用默认 label
-节点:
\myincludegraphics[0.25,0.35][0.75,0.65][ShowDefaultLabel]{example-image}
代码
\documentclass{article}
\usepackage{tikz}
\NewDocumentCommand{\myincludegraphics}{s O{0,0} O{1,1} m}{%
\IfBooleanTF{#1}{%
\noindent\includegraphics[width=1\textwidth]{#4}%
}{%
\begin{tikzpicture}
\node[anchor=south west, inner sep=0] (image) at (0,0) {\includegraphics[width=1\textwidth]{#4}};
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\node[anchor=center, red] (label) at (0.5,0.9) {my image description};%
\draw[red] (#2) rectangle (#3);
\end{scope}
\end{tikzpicture}%
}}
\begin{document}
\myincludegraphics*{example-image}
\myincludegraphics{example-image}
\myincludegraphics[0.25,0.35][0.75,0.65]{example-image}
\end{document}
输出
答案1
您可以像这样设置可选令牌:
\documentclass{article}
\usepackage{tikz}
\NewDocumentCommand{\myincludegraphics}{s O{0,0} O{1,1} t\displaytitle m}{%
\IfBooleanTF{#1}{%
\noindent\includegraphics[width=1\textwidth]{#5}%
}{%
\begin{tikzpicture}
\node[anchor=south west, inner sep=0] (image) at (0,0) {\includegraphics[width=1\textwidth]{#5}};
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\IfBooleanT{#4}{\node[anchor=center, red] (label) at (0.5,0.9) {my image description};}%
\draw[red] (#2) rectangle (#3);
\end{scope}
\end{tikzpicture}%
}}
\begin{document}
\myincludegraphics*{example-image}
\myincludegraphics{example-image}
\myincludegraphics[0.25,0.35][0.75,0.65]\displaytitle{example-image}
\end{document}
如果您想使用可选参数,您可以这样做(这样,您可以使用#4 定义标题):
\documentclass{article}
\usepackage{tikz}
\NewDocumentCommand{\myincludegraphics}{s O{0,0} O{1,1} d<> m}{%
\IfBooleanTF{#1}{%
\noindent\begin{tikzpicture}
\node[anchor=south west, inner sep=0] (image) at (0,0) {\includegraphics[width=1\textwidth]{#5}};
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\IfValueT{#4}{\node[anchor=center, red] (label) at (0.5,0.9) {#4};}%
\end{scope}
\end{tikzpicture}%%
}{%
\noindent\begin{tikzpicture}
\node[anchor=south west, inner sep=0] (image) at (0,0) {\includegraphics[width=1\textwidth]{#5}};
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\IfValueT{#4}{\node[anchor=center, red] (label) at (0.5,0.9) {#4};}%
\draw[red] (#2) rectangle (#3);
\end{scope}
\end{tikzpicture}%
}}
\begin{document}
\myincludegraphics*<A title>{example-image}
\myincludegraphics{example-image}
\myincludegraphics[0.25,0.35][0.75,0.65]<anything like a title>{example-image}
\end{document}