在 tikz 节点内使用 \hfill

在 tikz 节点内使用 \hfill

我想在 Tikz 节点中使用 \hfill,以便在某些节点的左侧和右侧显示一些文本。

看看下面的 MWE

\def\left{L\hfill}
\def\right{\hfill R}
\begin{tikzpicture} [sibling distance=4cm]
   \node { \left Prova\right}
        child {node {\left Second line - left}}
        child {node {Second line - right\right}
            child {node {\left Third line - left}}
            child {node {Third line \right}}
        };

\end{tikzpicture}

\left a\right

\left ab\right

\left abc\right

平均能量损失

我希望 Tikz 树的“L”和“R”与“a/ab/abc”段落处于同一级别。这可能吗?

我想获得这样的东西!不是什么重要的事情:出于某种原因,a、ab、abc 行不能作为 tikz 代码添加,而只能作为“普通”LaTeX 添加。这意味着 L 和 R 必须用 \hfill 定位...

我想获得什么

根据@Andrew,已找到解决方案。

最终代码可在此处获取

https://github.com/maieul/ledmac/blob/80031025472831820d2e3c3b27f579bff5163cdf/solution1.tex

最终代码

我希望提出一个eledtikz一揽子方案。

答案1

可能这更接近你想要的。当然生成的图片就是你要求的:

在此处输入图片描述

“子节点”的绘制基本上是使用您已有的内容完成的。唯一的区别是,我为一些子节点指定了节点名称。我在环境\foreach内的循环中“手工”绘制了 a、ab、abc,tikzpicture并为它们指定了名称,以帮助绘制它们LR标签。

游戏的乐趣在于实际在左侧和右侧绘制L和标签。这是通过循环遍历所有应该用或标记的节点名称来完成的。在您的示例中,这些可以合并到一个循环中,但我假设有些行只有一个标签,所以我使用了两个循环。要添加或删除这些标签,只需从循环中添加或删除相应行中的节点即可。R\foreachLR\foreach

重点是这些\foreach循环是唯一的地方您需要在其中指定应赋予左标记和右标记的行。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

  \begin{tikzpicture}[sibling distance=5cm,
                      edge from parent/.style={draw,blue!80,thick,->}]
   \node(A){Prova}
        child {node {Second line - left} }
        child {node (B) {Second line - right}
            child {node (C) { Third line - left}}
            child {node {Third line }}
        };

  % place the text below the kids
  \foreach \txt/\Y in {a/-4,ab/-5,abc/-6} {
    %\txt=label+anchor name, \Y=y-coord, \x1=x-coord of (B)
    \path let \p1 = (B) in node (\txt) at (\x1,\Y){\txt};
  }

  % loop over the "left" nodes
  \foreach \Node in {A,B,C,a,ab,abc} {%\y1=y-coord of the node
    \path let \p1=($ (\Node) $) in node at (-5,\y1){L};
  }

  % loop over the "right" nodes
  \foreach \Node in {A,B,C,a,ab,abc} {%\y1=y-coord of the node
    \path let \p1=($ (\Node) $) in node at (7,\y1){R};
  }

\end{tikzpicture}

\end{document}

为了循环遍历节点名称,我使用 calc 包来评估节点。这对我来说似乎有点夸张,但我找不到更好的方法,而且我记得在其他地方看到过类似的东西。使用\let获取节点 (x,y) 坐标的技巧来自 tikz 节点与另一个节点位于同一 x 坐标但指定了 y 坐标

答案2

嗯,一个非常粗鲁的尝试是:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{chains,positioning}
        \begin{document}
    \begin{tikzpicture}[
    node distance=0mm,
    start chain = going below,
Cnode/.style = {node contents={#1}, on chain},
Lnode/.style = {name=l#1, node contents={L}},
Rnode/.style = {name=r#1, node contents={R}},
sibling distance = 44mm,
                        ]
   \node (n11) {Prova}
        child {node (n21){Second line - left}}
        child {node (n22) {Second line - right}
            child {node (n31) {Third line - left}}
            child {node (n32) {Third line}}
        };
\node[Lnode=1, left=44mm of n11];
\node[Rnode=1,right=55mm of n11];
%---
\node[Lnode=2,left=of l1.east |- n21];
\node[Rnode=2,left=of r1.east |- n22];
%---
\node[Lnode=3,left=of l1.east |- n31];
\node[Rnode=3,left=of r1.east |- n32];
%---
    \foreach \i [count=\xi from 4] in {3,...,5}
\node[Lnode=\xi,below=of l\i];
\node[Cnode=a, below=of l4.north -| n22];
\node[Cnode=ab];
\node[Cnode=aby];
    \foreach \i [count=\xi from 4] in {3,...,5}
\node[Rnode=\xi,below=of r\i];
%-------
    \end{tikzpicture}
        \end{document}

答案3

在这个特殊情况下,可以使用tree空的构建所有内容root。下一个代码显示了如何使用forest这个方案有个问题就是幻影根节点占用的空间。

\documentclass[border=2mm]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest} for tree={edge={draw,blue!80,thick,->}}
[,
    [L, for tree={no edge} [L [L [L [L [L]]]]]]
    [Prova,  no edge [Second line-left ] 
                [Second line-right
                    [Third line-left] 
                    [, for tree={no edge} [a [ab [abc]]]] 
                    [Third line]]]
    [R , for tree={no edge} [R [R [R [R [R]]]]]]
]
\end{forest}
\end{document}

在此处输入图片描述

相关内容