5

I am struggling with inner node layout with Tikz. Basically, I want to add a line under the first text line inside a node, splitting it unevenly. Here is a try:

Two tikz nodes, one with a stupidly huge white space into its first part, and another not split

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes}

\begin{document}
    \begin{tikzpicture}[auto, node distance=3cm,
        style1/.style={ellipse split, draw, align=center},
        style2/.style={ellipse, draw, , align=center},
    ]

        \node[style1] (1) {NODE1 \nodepart{lower} long name \\ foo \\ bar \\ baz};

        \node[style2] (2) [right of=1] {NODE2 \\ long name \\ foo \\ bar \\ baz};

    \end{tikzpicture}
\end{document}

The split option seems to be able to split the ellipsis in half, leaving an enormous white space above the first line of text (see NODE1). I wish to render something like NODE2, but with a line separating the first line from the others. Here is an example of the desired output :

The desired output

I tried to add \hrule command just after the "NODE2" text, but it does not seem to be possible. Do you have any idea on how to do this?

By the way, please note that I saw this related question and its accepted answer, but I believe it does not address my issue.

  • 1
    Can you add a picture of the desired output? Maybe draw it by hand if you cannot provide a digital version. – Dr. Manuel Kuehner Feb 25 at 13:42
5

Something of that sort?

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes,fit,positioning}

\begin{document}
    \begin{tikzpicture}[auto, node distance=3cm,
        style1/.style={ellipse split, draw, align=center},
        style2/.style={ellipse, draw, , align=center},
        lower elli/.style={align=center},
        fit elli/.style={ellipse,draw,inner sep=0pt,
        path picture={\draw ([xshift=-1cm]#1.south west)-- 
        ([xshift=1cm]#1.south east);}}
    ]

        \node[style1] (1) {NODE1 \nodepart{lower} long name \\ foo \\ bar \\ baz};

        \node[style2] (2) [right=1cm of 1] {NODE2 \\ long name \\ foo \\ bar \\ baz};

        \node (3a) [right=3.5cm of 2.north,anchor=north] {NODE3};
        \node[lower elli] (3b)[below=0pt of 3a] {long name \\ foo \\ bar \\ baz};
        \node[fit elli=3a,fit=(3a) (3b)]{};
    \end{tikzpicture}
\end{document}

enter image description here

  • This seems a bit hard to position and align with other nodes if that is required by the OP (e.g. [right of=1] will not have them vertically aligned anymore) – sheß Feb 25 at 14:32
  • 2
    @sheß Vertical alignment is not really an issue to me. But it might be for someone, indeed. – Lyudline Feb 25 at 14:34
  • @sheß If you want to position relative to each other, there is no problem. You could then position the upper parts relative to each other. – marmot Feb 25 at 14:37
  • True. Though that would still change behavior compared to a normal node, especially when nodes vary in size. But this seems not to be an issue for the OP – sheß Feb 25 at 14:47
6

You could just draw a longer [h]rule and clip the node:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes}
\def\clap#1{\hbox to 0pt{\hss#1\hss}}
\begin{document}
\begin{tikzpicture}[auto, node distance=3cm,
    styleshess/.style={ellipse, draw, align=center,clip},
]
    \node[styleshess] (2) {NODE2\\\clap{\rule{\linewidth}{.4pt}}\\ long name \\ foo \\ bar \\ baz};
\end{tikzpicture}
\end{document}

enter image description here

You can play around with that line as with any other \rule to modify positioning or width. The \clap makes sure that the (imaginary) length of that line does not blow up the size of the node. The clip in the style definition makes sure that only the part of the line that falls within the node is actually drawn.

  • 2
    This seems to have a larger gap than in the screen shot of the OP. (Personally I do not like these kinds of comments, but you started that.) – marmot Feb 25 at 14:36
  • That solution is closest to what I tried, but I prefer the breakdown into two nodes suggested by @marmot : I feel I can control more easily the style of each subpart of the ellipsis. The code is more elegant to me, however. – Lyudline Feb 25 at 14:40
  • That space is easily adjustable with the usual techniques as pointed out in my post. You could write NODE2\\[-0.5\baselineskip]\clap{... or something of that sort to reduce that space. I didn't intend to be nasty or start a game of tit-for-tat with my comment on your post, I just thought easy positioning might be a factor for people drawing nodes in tikz. Nevermind. – sheß Feb 25 at 14:43
  • You could just add a tabular instead. (These kinds of shapes generally have issues when relative positioning unless you position relative to their centers.) – marmot Feb 25 at 14:50
  • I am not sure I understand. Do you mean add a tabular inside the node? What do you mean by "[t]hese kinds of shapes"? – sheß Feb 26 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.