我有一系列复杂的内容,我想使用 TikZ 来呈现。代码非常规则,所以我想编写一个简单的命令来处理所有事情。在下面的代码中,\aelabelA
和\aelabelC
根本无法正常工作,我不明白为什么。但是,\aelabelB
确实有效。有人能解释一下为什么场景 A 和 C 的框内容不可见吗?
\documentclass{standalone}
\usepackage{xparse}
\usepackage{tikz}
\ExplSyntaxOn
\newsavebox\aebox
\NewDocumentCommand\aelabelA{ mu{~located~at~(}u{);} }
{
\begin{lrbox}{\aebox}
\begin{minipage}{1in}
\Large\ttfamily\bfseries
\centering\makebox[0pt]{#1}
\end{minipage}
\end{lrbox}
\node[draw] ~ at~ (#3) ~ {\usebox{\aebox}};
}
\NewDocumentCommand\aelabelB{ m }
{
\begin{lrbox}{\aebox}
\begin{minipage}{1in}
\Large\ttfamily\bfseries
\centering\makebox[0pt]{#1}
\end{minipage}
\end{lrbox}
\usebox{\aebox}
}
\ExplSyntaxOff
\def\aelabelC#1 located at (#2);{%%
\begin{lrbox}{\aebox}
\begin{minipage}{1in}
\Large\ttfamily\bfseries
\centering\makebox[0pt]{#1}
\end{minipage}
\end{lrbox}%%
\node[draw] at (#2) {\usebox{\aebox}};
}
\begin{document}
\begin{tikzpicture}
\coordinate (AA) at (0,0);
\coordinate (BB) at (2,2);
\coordinate (CC) at (4,4);
\aelabelA {Keys} located at (AA);
\node[draw] at (BB) {\aelabelB{Keys}};
\aelabelC{Keys} located at (CC);
\end{tikzpicture}
\end{document}
答案1
钛钾众所周知,Z 会吞食tikzpicture
环境中的普通物质除非这个东西在节点中。你可以中断tikzpicture
以避免被忽略,并且你需要一点全球化才能使其工作。我以这种方式修复了你的第一个命令,但出于教学目的保留了最后一个命令。
\documentclass{standalone}
\usepackage{xparse}
\usepackage{tikz}
\ExplSyntaxOn
\newsavebox\aebox
\NewDocumentCommand\aelabelA{ mu{~located~at~(}u{);} }
{\begin{pgfinterruptpicture}
\begin{lrbox}{0\null\global\setbox\aebox} % from https://tex.stackexchange.com/questions/49129/lrbox-in-newenvironment/49136#49136
\begin{minipage}{1in}
\Large\ttfamily\bfseries
\centering\makebox[0pt]{#1}
\end{minipage}
\end{lrbox}
\end{pgfinterruptpicture}
\node[draw] ~ at~ (#3) ~ {\usebox{\aebox}};
}
\NewDocumentCommand\aelabelB{ m }
{
\begin{lrbox}{\aebox}
\begin{minipage}{1in}
\Large\ttfamily\bfseries
\centering\makebox[0pt]{#1}
\end{minipage}
\end{lrbox}
\usebox{\aebox}
}
\ExplSyntaxOff
\def\aelabelC#1 located at (#2);{%%
\begin{lrbox}{\aebox}
\begin{minipage}{1in}
\Large\ttfamily\bfseries
\centering\makebox[0pt]{#1}
\end{minipage}
\end{lrbox}%%
\node[draw] at (#2) {\usebox{\aebox}};
}
\begin{document}
\begin{tikzpicture}
\coordinate (AA) at (0,0);
\coordinate (BB) at (2,2);
\coordinate (CC) at (4,4);
\aelabelA {Keys} located at (AA);
\node[draw] at (BB) {\aelabelB{Keys}};
\aelabelC{Keys} located at (CC);
\end{tikzpicture}
\end{document}
编辑:@egreg(mille grazie!)提供了一个稍微更干净的替代方案,具有相同的输出。
\documentclass{standalone}
\usepackage{xparse}
\usepackage{tikz}
\ExplSyntaxOn
\newsavebox\aebox
\NewDocumentCommand\aelabelA{ mu{~located~at~(}u{);} }
{\begin{pgfinterruptpicture}
\begin{lrbox}{0}%
\begin{minipage}{1in}%
\Large\ttfamily\bfseries
\centering\makebox[0pt]{#1}%
\end{minipage}%
\end{lrbox}%
\global\setbox\aebox=\box0
\end{pgfinterruptpicture}
\node[draw] ~ at~ (#3) ~ {\usebox{\aebox}};
}
\NewDocumentCommand\aelabelB{ m }
{
\begin{lrbox}{\aebox}
\begin{minipage}{1in}
\Large\ttfamily\bfseries
\centering\makebox[0pt]{#1}
\end{minipage}
\end{lrbox}
\usebox{\aebox}
}
\ExplSyntaxOff
\def\aelabelC#1 located at (#2);{%%
\begin{lrbox}{\aebox}
\begin{minipage}{1in}
\Large\ttfamily\bfseries
\centering\makebox[0pt]{#1}
\end{minipage}
\end{lrbox}%%
\node[draw] at (#2) {\usebox{\aebox}};
}
\begin{document}
\begin{tikzpicture}
\coordinate (AA) at (0,0);
\coordinate (BB) at (2,2);
\coordinate (CC) at (4,4);
\aelabelA {Keys} located at (AA);
\node[draw] at (BB) {\aelabelB{Keys}};
\aelabelC{Keys} located at (CC);
\end{tikzpicture}
\end{document}
在\savebox
tikzpicture
已创建于这个问题。