2

EDIT: I'd like to set the yshift of a node to be some factor of the number of lines in the node's contents. In trying to determine the number of lines, I originally wrote the question below.

EDIT A partial solution, based on the answer of Schrödinger's cat, is below with figure and code. It is not fully automated, specifying boxes and the use of \pgftruncatemacro before each node is not desirable.

enter image description here

\documentclass[margin=6]{standalone}

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

\begin{document}

\newbox\mybox\setbox\mybox=\vbox{\hbox{foo}\hbox{bar}\hbox{bazzy}}
\newbox\myboxb\setbox\myboxb=\vbox{\hbox{\ttfamily abc\#xyz}}

\begin{tikzpicture}[
  card/.style={
    draw,
    dashed,
    anchor=base west,
    inner sep=0,
    append after command={[/utils/exec=\let\mytikzlastnode\tikzlastnode]
      node [
      below=5mm of \tikzlastnode.base east,
      anchor=base east,
      font=\scriptsize,
      inner sep=0
      ] (mynode) {#1}
      node [draw,fit={(\mytikzlastnode) (mynode.base east)}] {}
    },
  }
  ]

  \draw (-1,0) -- (5,0);

  \pgfmathtruncatemacro{\myn}{(\dp\mybox+\ht\mybox+2pt)/11.5pt}\myn
  \node [card=num1,yshift=-(\myn-1)*\baselineskip] (A) {\box\mybox};

  \pgfmathtruncatemacro{\myn}{(\dp\myboxb+\ht\myboxb+2pt)/11.5pt}\myn
  \node [card=c2,right=1cm of A,yshift=-(\myn-1)*\baselineskip] (B) {\box\myboxb};

\end{tikzpicture}

\end{document}

Original question: I'd like to attach the number of lines in a node's contents as an "auxiliary" node, MWE below.

I have a candidate solution to count the number of lines of something from this answer, but I cannot parse the code well enough to untangle that solution from \title. I have not been able to find a way to access the node contents from the style.

\documentclass[margin=6]{standalone}

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

\begin{document}
\begin{tikzpicture}[
  card/.style={
    draw,
    dashed,
    inner sep=0,
    append after command={
      \pgfextra{
        \node [
        below=5mm of \tikzlastnode.base east,
        anchor=base east,
        font=\scriptsize,
        inner sep=0
        ] (mynode) {????}; % get node contents and count lines
        \node [draw,fit={(\tikzlastnode) (mynode.base east)}] {};
      }
    }
  }
  ]

  \node [card] (C) {foobar\\baz};

\end{tikzpicture}

\end{document}

1 Answer 1

1

I do not know a 100% fool proof way of measuring the number of lines with TikZ. What if you have, say, a large integral sign? However, for normal lines you could just measure the height and infer the number from this height. BTW, do not use \pgfextra in paths.

\documentclass[margin=6]{standalone}

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

\begin{document}
\begin{tikzpicture}[
  card/.style={align=center,
    draw,
    dashed,
    inner sep=0,
    append after command={[/utils/exec=\let\mytikzlastnode\tikzlastnode]
    let \p1=($(\mytikzlastnode.north)-(\mytikzlastnode.south)$) in 
     node [
        below=5mm of \tikzlastnode.base east,
        anchor=base east,
        font=\scriptsize,
        inner sep=0
        ] (mynode) {\pgfmathtruncatemacro{\myn}{(\y1+5pt)/11.5pt}\myn} % get node contents and count lines
        node [draw,fit={(\mytikzlastnode) (mynode.base east)}] {}
    }
  }
  ]

  \draw node [card] (A) {foobar};

  \draw (3,0) node [card] (B) {foobar\\baz};

  \draw (6,0) node [card] (C) {foobar\\baz\\blub};

  \draw (9,0) node [card] (D) {foobar\\baz\\blub\\pft};

\end{tikzpicture}

\end{document}

enter image description here

1
  • This is clever. I dumbed down my MWE a little too far from what I actually want to achieve, I guess. I really want to be able to align these card objects by the baseline of the first line of text, so what I was thinking was to anchor a new object at base (baseline of the last line), compute the number of lines less 1, multiply times \baselineskip, then set the result as the yshift in the node style. I'm still learning about append after..., execute after... etc. Is it possible to do the calculation you provided, but set the result as yshift of the initial node?
    – tsj
    Commented Nov 4, 2019 at 14:49

You must log in to answer this question.

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