如何使 tikzpicture 内部的函数表现得像外部一样

如何使 tikzpicture 内部的函数表现得像外部一样

我有一个宏,它生成一个结构(规则)和对该结构的引用。它在内部时不起作用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}

示例输出

相关内容