tikzpicture 中的新运算符

tikzpicture 中的新运算符

我正在尝试声明一个新的运算符(仅使用水平和垂直线和标签)。现在我使用tikzpicture以下代码来实现它:

\newcommand{\newtypeline}[3]{
\begin{tikzpicture}[scale=2]%
\draw (0,0) -- (10ex,0ex);%
\draw (5ex,-1ex) -- (5ex,1ex);
\node[label=left:{$#1$}] at (0,0 ) {};
\node[label=above:{$#2$}] at (5ex,1ex ) {};
\node[label=below:{$#3$}] at (5ex,-1ex ) {};
\end{tikzpicture}%
}

问题是我想在另一个tikz环境中使用该新命令 - 并且两个tikzs 环境开始相互交互。有没有办法声明一个运算符以便稍后嵌入到tikzpictures 中?

编辑:

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{automata,arrows,shapes,snakes,topaths,trees,backgrounds}
\newcommand{\newtypeline}[3]{%
\begin{tikzpicture}% 
\draw (0,0) -- (10ex,0ex);% \draw (5ex,-1ex) -- (5ex,1ex); 
\node[label=left:{$#1$}] at (0,0 ) {}; 
\node[label=above:{$#2$}] at (5ex,1ex ) {}; 
\node[label=below:{$#3$}] at (5ex,-1ex ) {}; 
\end{tikzpicture}% 
} 
\begin{document} 
intend to look like: 
\newtypeline{a}{b}{c} looks like: 
\begin{tikzpicture}[->] 
\node at (0,1) {\newtypeline{a}{b}{c}}; 
\end{tikzpicture} 
\end{document}

答案1

您可以定义您的命令没有TikZ 避免嵌套的危险和副作用tikzpicture;你可以这样做:

\documentclass[11pt]{article}
\usepackage{tikz}
\usepackage{varwidth}
\usetikzlibrary{automata,arrows,shapes,snakes,topaths,trees,backgrounds}

\newcommand{\newtypeline}[3]{%
\begin{varwidth}{\linewidth}
$#1$
\end{varwidth}~%
\begin{varwidth}{\linewidth}
\centering
\raisebox{0.5ex}{$#2$}\\
\rule[1ex]{10ex}{0.4pt}\\
\raisebox{0ex}{$#3$}
\end{varwidth}% 
} 

\begin{document} 

intend to look like: 
\newtypeline{a}{b}{c} looks like: 
\begin{tikzpicture}[->] 
\node at (0,1) {\newtypeline{a}{b}{c}}; 
\end{tikzpicture} 

\end{document}

在此处输入图片描述

在评论中提出新要求后:

\documentclass[11pt]{article}
\usepackage{tikz}
\usepackage{varwidth}
\usetikzlibrary{automata,arrows,shapes,snakes,topaths,trees,backgrounds}

\newcommand{\newtypeline}[3]{%
\begin{varwidth}{\linewidth}
$#1$
\end{varwidth}~%
\begin{varwidth}{\linewidth}
\centering
\raisebox{0.5ex}{$#2$}\\
\rule[1ex]{10ex}{0.4pt}\rlap{\hspace*{-5ex}\rule{0.4pt}{2ex}}\\
\raisebox{0ex}{$#3$}
\end{varwidth}% 
} 

\begin{document} 

intend to look like: 
\newtypeline{a}{b}{c} looks like: 
\begin{tikzpicture}[->] 
\node at (0,1) {\newtypeline{a}{b}{c}}; 
\end{tikzpicture} 

\end{document}

在此处输入图片描述

答案2

最后,我决定明确声明所有参数,以覆盖从外部 tikzpicture 层继承的参数。此外,还进行了一些裁剪,完成了我的工作。我使用的最终定义是:

\newcommand{\newtypeline}[3]{
\mbox{
\begin{tikzpicture}[-,>=stealth',shorten >=0pt,auto,node distance=0cm,semithick,scale=1]
\clip(-3ex,-2.5ex) rectangle (26.5ex,3.2ex);
\draw (0,0) -- (24ex,0ex);%
\draw (12ex,-0.8ex) -- (12ex,0.8ex);
\node[label={}] at (-2ex,0 ) {$#1$};
\node[label={}] at (12ex,2ex ) {$#2$};
\node[label={}] at (12ex,-2ex ) {$#3$};
\end{tikzpicture}
}}
}

相关内容