我有一个宏,它生成一个结构(规则)和对该结构的引用。它在内部时不起作用tikzpicture
。如何使函数即使在 tikzpicture 内部也能引用该结构?(我还想摆脱错误消息“mevp 未定义”)
\documentclass[12pt,a4paper]{book}
\makeatletter
%\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{ifthen}
\usepackage{tikz}
\newcounter{strctz}
\DeclareRobustCommand{\strct}[1]{%
% \ifthenelse{\isundefined{\c#1}} %
% {
\refstepcounter{strctz}%
\textbf{#1}\textbf{\Large({\thestrctz})}%
\\[2mm]%
\rule{2cm}{5mm}%
\begingroup
\expandafter\protected@edef\csname c#1\endcsname{\thestrctz}%
\expandafter\endgroup
\expandafter\DeclareRobustCommand
\csname c#1\expandafter\expandafter\expandafter\endcsname
\expandafter\expandafter\expandafter{\csname c#1\endcsname}%
% }
% {#1%
% \\[2mm]%
% \rule{2cm}{5mm}%
% (\c#1)%
% }
}
\makeatother
\begin{document}
\strct{mevalonate}
(\cmevalonate) and
\strct{ohpregnenolone}
Structure (\cohpregnenolone) and structure (\cmevalonate) and
Structure (\cohpregnenolone)
\strct{alanine} and (\calanine) (\cmevalonate) (\cohpregnenolone)
\strct{mevalonate}
%Isoprenyl-Bildung, Dimethyl-Allyl-Bildung aus Mevalonat
%
\tikzstyle{prenPP}= [rectangle,minimum size=2.4cm,text width=3.8cm,text centered,inner sep=.05cm]
\begin{tikzpicture}
\node [prenPP%
,text width=4.6cm%
] (mev) at (0,8) {\strct{mevalonate}};
node [prenPP%
,text width=4.6cm%
] (mevP) at (8,8) {\strct{mevalonateP}};
\draw[->,shorten >=0.5cm,shorten <=0.5cm,very thick]
(mev) -- node[below=0.5pt] {\parbox{2.5cm}{%
\begin{center}%
enzym{mevalonate kinase}
\end{center}}}%
(mevP) ;
\end{tikzpicture}
\cmelonalonateP
\end{document}
答案1
基本上这是一个拼写错误 - 您在最后调用的宏\cmelonalonateP
不是\cmevalonateP
节点中定义的宏 。此外,未知形状是由于\
在相应的定义节点命令之前缺少 。
但是,ifthenelse
使用包中的宏来代替会更好etoolbox
。这将允许您缩短代码并使其更清晰。特别是etoolbox
提供\ifcsdef
,\csuse
以及\protected@csxdef
除其他外,它们与宏名称一起使用,而您不必发出\csname...\endcsname
并跟踪扩展。
例如
\ifcsdef{c#1}{TRUE}{FALSE}
\
检查所跟的宏c
以及的内容是否#1
已定义,并采取相应的措施。
\documentclass[12pt,a4paper]{book}
\usepackage{graphicx}
\usepackage{tikz,etoolbox}
\newcounter{strctz}
\makeatletter
\DeclareRobustCommand{\strct}[1]{%
\ifcsdef{c#1}{%
#1\\[2mm]%
\rule{2cm}{5mm}%
(\csuse{c#1})%
}{%
\refstepcounter{strctz}%
\textbf{#1}\textbf{ -- def. ({\thestrctz})}%
\\[2mm]%
\rule{2cm}{5mm}%
\protected@csxdef{c#1}{\thestrctz}%
\expandafter\DeclareRobustCommand
\csname c#1\expandafter\expandafter\expandafter\endcsname
\expandafter\expandafter\expandafter{\csname c#1\endcsname}%
}%
}
\makeatother
\begin{document}
\parindent=0pt
\strct{mevalonate} (\cmevalonate) and
\strct{ohpregnenolone}
Structure (\cohpregnenolone) and structure (\cmevalonate) and
structure (\cohpregnenolone)
\strct{alanine} and (\calanine) (\cmevalonate) (\cohpregnenolone)
\strct{mevalonate}
\tikzstyle{prenPP}=[rectangle, minimum size=2.4cm, text width=3.8cm,
text centered, inner sep=.05cm]
\begin{tikzpicture}
\node [prenPP,text width=4.6cm]
(mev) at (0,8) {\strct{mevalonate}};
\node [prenPP,text width=4.6cm]
(mevP) at (8,8) {\strct{mevalonateP}};
\draw[->,shorten >=0.5cm,shorten <=0.5cm,very thick]
(mev) -- node[below=0.5pt] {\parbox{2.5cm}{%
\begin{center}%
enzym{mevalonate kinase}
\end{center}}}%
(mevP) ;
\end{tikzpicture}
Here we see (\cmevalonateP) is defined.
\end{document}