4

I would like to use an \hfill in a Tikz node, in order to have some text on the left and right of some node.

Look at the following 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

MWE

I would like the "L" and "R" of the Tikz tree to be at the same level as those of "a/ab/abc" paragraph. Is it possible?

I would like to obtain something like this! Not an important thing: for some reason, the a, ab, abc line can't be added as tikz code, but only as "normal" LaTeX. That means the L and R must be positioned with \hfill...

What I would like to obtain

Solution was found, based on the @Andrew.

Final code is available here

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

Final code

I hope to propose an eledtikz package.

5
  • 1
    The use of \hfill has sense only if the node text width is defined. Otherwise, node width is adopted to width of text inside node, which is placed according to defined text align (left,center, right, ...)
    – Zarko
    Commented Aug 14, 2014 at 18:06
  • ok, In understand. But is there any solution for this problem? have the R aligned and the L aligned? It doesn't matter if by \hfill or other way...
    – Maïeul
    Commented Aug 14, 2014 at 18:10
  • I'm n0t sure if I understood you you correctly. If you like to have aligned text on left side of node, then use node option align=left, for the align on the right, use align=right, etc. More about text align you can find in tikz manual, pp.223 -- 228 (for tikz 3.0.0).
    – Zarko
    Commented Aug 14, 2014 at 20:12
  • no, I don't want to have aligned text on Left side of node or in right side. Do you seen the column with the R? I will like to have the R of the tikz in the same column, and the same thing for the L...
    – Maïeul
    Commented Aug 14, 2014 at 20:15
  • I have added a figure of what I would like to obtain. With the R and L aligned vertically.
    – Maïeul
    Commented Aug 14, 2014 at 20:23

3 Answers 3

4

Possibly this is closer to what you want. Certainly the picture produced is what you ordered:

enter image description here

The drawing of the "children" is done using essentially what you had. The only difference is that I have given node names to some of the children. I have drawn the a, ab, abc "by hand" inside a \foreach loop inside the tikzpicture environment and given them names to help draw their L and R labels.

The fun and games comes in actually drawing the L and R labels down the left and right hand sides. This is done using a \foreach loop over all of the node names that should be marked with an L or with an R. In your example these could be combined into one loop but I assumed that there would be some rows with only one label so I used two loops. To add or remove these labels you just add or remove a node in the corresponding row from the \foreach loop.

The point is that these \foreach loops are the only place where you need to specify the rows that should be given left and right markings.

\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}

To loop over the node names I use the calc package to evaluate the nodes. This seems a little OTT to me but I couldn't find a better way of doing this, and I remembered seeing something like this somewhere else. The trick of using \let to get the (x,y)-coordinates of the nodes comes from tikz-node-at-same-x-coordinate-as-another-node-but-specified-y-coordinate.

9
  • that look closer what I need. I have just to try to adapt it to me generalist case case. I will try and send you an answer
    – Maïeul
    Commented Aug 15, 2014 at 8:57
  • thanks a lot ! I hope this code will help me to solve a generaliste problem with eledmac
    – Maïeul
    Commented Aug 15, 2014 at 8:58
  • Ok, I have look to it. That is nice, but there are still a problem ... left/right position are fixe (-5,7) and not calulated depending of the page margin.
    – Maïeul
    Commented Aug 15, 2014 at 11:03
  • Your code is very nice, it produce the expected result, but for some reason, the a, ab, abc lines must be print without tikz, but with "normal" LaTeX
    – Maïeul
    Commented Aug 15, 2014 at 11:04
  • changing your code to adapt to my specific needs, I have found the way. The final code is here github.com/maieul/ledmac/blob/…
    – Maïeul
    Commented Aug 15, 2014 at 11:32
2

well, a very rude attempt is:

\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}
6
  • indeed, that's working. But, my problem is more complex, because I need to do it for many case in need to have only two commands Left/Right to be inserted on Left/Right of the node where I want to have the L/R.
    – Maïeul
    Commented Aug 14, 2014 at 22:25
  • That is for adding it in eledmac package. Cf github.com/maieul/ledmac/blob/… and github.com/maieul/ledmac/issues/191
    – Maïeul
    Commented Aug 14, 2014 at 22:26
  • So what I would like is a command which said "from this point, go left/right to the page margin"
    – Maïeul
    Commented Aug 14, 2014 at 22:28
  • I newer use ledmac, but I think that it can add comment to particular line of text and that it consider a picture as one line. Therefore, your comments of lines in picture probably should be part of picture. There, if you like, that the comment are outside of text width (if I understood you correctly), the picture should be wider than text and you need to define node style such, that they include left and right comments as tikz pictures at appropriate coordinate. Defining such a style is not simply (for me), but can be done. if I will have dome sparse time, I will try to do this.
    – Zarko
    Commented Aug 14, 2014 at 22:47
  • the problem, for me, is not the comment. That is working well with eledmac (and I can do it). The problem is the lins number. I can step line number in each "level" of the picture, but what I need it to push it on the margin. That is the point. In my MWE, I have changed line number with L/R, to be "minimalist"
    – Maïeul
    Commented Aug 14, 2014 at 22:51
1

In this particular case is possible to build everything as a tree with an empty root. Next code shows how to do it with forest. There is a problem with this solution which is the space occupied by the phantom root node.

\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}

enter image description here

1
  • thanks for your code. But indeed, in my problem, it could be some other kind of tikz graph that tree...
    – Maïeul
    Commented Aug 25, 2014 at 13:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .