我正在处理一个文档,其中我使用 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}