具有可变长度/宽度的节点 xshift

具有可变长度/宽度的节点 xshift

我是 LaTeX 和 tikz 的新手。我想生成一个块图,块之间的宽度可变,具体取决于它们之间的箭头的文本。当我使用常量时xshift,一切都按预期工作。一旦我尝试使用\settowidth{\mylength}{dummytext}\the\mylength作为语句的参数xshift,我的调整就不起作用。

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}
\usepackage{calc}
\newlength{\mylength}
%First Block
\newcommand{\startblock}[3]{
    % Place nodes
    \settowidth{\mylength}{#1}
    \node [block, label={[label distance=.2pt]270:#2}] (#2){};
% xshift = -65pt will produce my desired result
    \node [virtual, left = of #2, xshift=-\the\mylength] ({#2}i)     {};
    \node [virtual, right=of #2] ({#2}o)    {};
    % Connect nodes
    \draw [->] ({#2}i) -- node {#1} (#2);
    \draw [->] (#2) -- node {#3}({#2}o);
    %\draw [-] ({\thecblock}.south west) -- ({\thecblock}.north east);
}
%Other Blocks
\newcommand{\appendblock}[3]{
    % Place nodes
    \node [block, right={of #1}, label={[label distance=.2cm]270:{#2}}] (#2){};
    \node [virtual, right=of #2] ({#2}o)    {};
    % Connect nodes
    \draw [->] (#2) -- node {#3}({#2}o);
    %\draw [-] ({\thecblock}.south west) -- ({\thecblock}.north east);
}
\begin{document}
\settowidth{\mylength}{VerylongInput}
\the\mylength
    \tikzstyle{block} = [draw, rectangle, minimum height=3em, minimum width=5em]
    \tikzstyle{virtual} = [coordinate]
    \begin{tikzpicture}[>=stealth,auto]%, node distance=2cm]
        \startblock{VerylongInput}{Block1}{Output}
        \appendblock{Block1}{Block2}{Raus2}
        %\testap{Block2}{Toll!}{Scheisse}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

在此处输入图片描述

答案1

在此处输入图片描述

\documentclass[border=1mm,tikz]{standalone}
\usetikzlibrary{arrows,positioning}    
\begin{document}

\newcommand{\placearrow}[3]{% {arrow start point}{label above arrow}{label name}
\node [anchor=south west] (#3) at (#1) {#2};
\draw [->] (#1)--(#3.south east);
}
\newcommand{\appendblock}[3]{% {previous label name}{block label}{bloack name}
\node [block,anchor=west] (#3) at (#1.south east){};
\node [anchor=north] at (#3.south) {#2};
}
\begin{tikzpicture}[>=stealth,block/.style={draw, minimum height=3em, minimum width=5em}] 

\placearrow{0,0}{Very long input}{n1}; \appendblock{n1}{Block 1}{B1}
\placearrow{B1.east}{Output}{n2}; \appendblock{n2}{Block 2}{B2}
\placearrow{B2.east}{Raus2}{n3}; 

\end{tikzpicture}

\end{document}

无需过度复杂化,您可以像上面一样实现自动解决方案,而无需。但是,原始代码中的主要错误是新宏中的calc计算;它应该改为读取。\settowidth{\mylength}{#1}\setlength{\mylength}{\widthof{#1}}

相关内容