3

In the following example, I would like to set the font size of a node from "inside" the node's contents, i.e. without changing the node's font attribute:

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}

%% From https://tex.stackexchange.com/q/359189/2113
\def\gobblechar{\let\xchar= }
\def\assignthencheck{\afterassignment\xloop\gobblechar}
\def\xloop{%
  \ifx\relax\xchar
      \let\next=\relax
    \else
      \phantom{8}\makebox[0pt][r]{\xchar}
      \let\next=\assignthencheck
  \fi
  \next}
\def\cellbits#1#2{\assignthencheck#1\relax \\ \assignthencheck#2\relax}

\begin{scope}[scale=0.5,every node/.style={font=\large,align=center}]
  \draw[thin, densely dotted] (0,0) grid (7,1);

  \begin{scope}[shift={(0.5,0.5)}]
    \draw (0,0) node{1};
    \draw (2,0) node{\cellbits{123}{456}};
    \draw (4,0) node[font=\tiny]{\cellbits{123}{456}};
    \draw (6,0) node{\tiny\cellbits{123}{456}};
  \end{scope}
\end{scope}

\end{tikzpicture}
\end{document}

The nodes at (0,0) and (2,0) are just for reference. The node at (4,0) is exactly the output I would like to get.

The node at (6,0) illustrates my problem. The \tiny size seems to be applied only to the first line (until the \\), and the line spacing and the second line all have size \large, i.e. the one based on the node's default font attribute:

Illustration of the problem

What can I put inside the (6,0) node's contents to get the same output as the (4,0) node?

P.s.: If it can't be done "compositionally" i.e. if I have to change the definition of cellbits / xloop to "push in" the \tiny-ness, that is not optimal, but still fine.

7
  • Dirty hack: \draw (6,0) node{\tiny\AddToHook{cmd/phantom/before}{\tiny}\cellbits{123}{456}\RemoveFromHook{cmd/phantom/before}}; Commented Feb 3 at 12:56
  • @samcarter_is_at_topanswers.xyz when I try that, it also puts everything in one line, i.e. it gets rid of the line break between the 3 and the 4. Commented Feb 3 at 13:04
  • Why don't you just define and use one or more styles for nodes? Commented Feb 3 at 13:08
  • The line spacing is off, but I do get a line break: i.sstatic.net/CNZkrkAB.png Commented Feb 3 at 13:08
  • 1
    Will \draw (6,0) node{\parbox{.03\textwidth}{\tiny\cellbits{123}{456}}}; suffice? Commented Feb 3 at 13:50

1 Answer 1

2

It is easy to save and restore the font size and baseline skip. However, doing that does not ensure the line-spacing is correct because line spacing takes effect only when a paragraph is set, which happens at the end and no such ending takes place within the node in this case.

One way around this, as suggested in comments, is to box the contents. However, \parbox requires the width of the requested box be explicitly set. There are various alternatives at this point, but a simple \vbox with two \hboxes seems to work well enough.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  %% From https://tex.stackexchange.com/q/359189/2113
  \def\gobblechar{\let\xchar= }
  \def\assignthencheck{\afterassignment\xloop\gobblechar}
  \def\xloop{%
    \ifx\relax\xchar
      \let\next=\relax
    \else
      \phantom{8}\makebox[0pt][r]{\xchar}%
      \let\next=\assignthencheck
    \fi
  \next}
  \def\cellbits#1#2{%
    \vbox{%
      \hbox{%
        \assignthencheck#1\relax
      }%
      \hbox{%
        \assignthencheck#2\relax
      }%
    }%
  }
  \begin{scope}[scale=0.5,every node/.style={font=\large,align=center}]
    \draw[thin, densely dotted] (0,0) grid (7,1);
    \begin{scope}[shift={(0.5,0.5)}]
      \draw (0,0) node{1};
      \draw (2,0) node{\cellbits{123}{456}};
      \draw (4,0) node[font=\tiny]{\cellbits{123}{456}};
      \draw (6,0) node{\tiny\cellbits{123}{456}};
    \end{scope}
  \end{scope}
\end{tikzpicture}
\end{document}

matching nodes (3 and 4)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.