# Text in TikZ nodes: controlling the box variables of characters/string

I remember that there were different commands in TikZ that allow modifying how the characters in a node are placed but I'm not able to find all of them in TikZ manual. Probably I'm looking for the wrong definition of what I'm searching.

See this example picture that depicts the box of a character:

One problem is the vertical alignment of two text nodes. If one text has descending characters like 'p', 'g', etc. while the other has none the two texts will be not vertical aligned. A solution is to set each node (or only those that give problems) with a fixed text depth, e.g. text depth=0ex or text depth=0.25ex1. And this is my first point of ignorance: what about ascending characters like 't', 'b' or capital characters? How is the character box in these cases? I didn't find any example. I guess that is only the height that change but I'm not sure.

The second problem is the horizontal alignment. Of course, it doesn't exist for monospaced fonts but in the other cases exist. How could set the text/character box to a fixed width? Use case: I have a node with text $S_0$ and one with text S_t; if I left align them there is no problem else if I right align them the S is misaligned.

In general, could you provide the commands that handle the box size of character/text in TikZ as well as in plain LaTeX?

Note The probability that I've written wrong things and that the title itself is wrong is very high. Feel free to edit my question or to provide good references to how characters/text boxes are handled.

Example

I've added more 0 and t as subscript do evidence the difference. Of course, this is the expected and right behaviour, i.e. the last two nodes have the text correctly right aligned. In my case, I have slides where the subscript of S changes but a large part of the figure remains the same. So, in this use case, I wish to have the S in the same position as before but with a different subscript.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node [align=left] (first) {long text\\ $S_{000}$};
\node [below=1em of first, align=left] (second) {long text\\ $S_{ttt}$};
\node [below=1em of second, align=right] (third) {long text\\ $S_{000}$};
\node [below=1em of third, align=right] (fourth) {long text\\ $S_{ttt}$};
\end{tikzpicture}
\end{document}


Further note This example is just a use case. I'm interested in ways to change the variables relative to boxes. In this case, the change would be to make t having the same box width of 0. Alternatively, to make the box of $S_t$ having the same box width of $S_0$.

• Could you provide an MWE for the use case with the S_0 and S_t? – Marijn Dec 13 '18 at 11:44
• $S_0$ and $S_t$ have different widths; if you right align them, the two S's will not be aligned to each other. How could they be? – egreg Dec 13 '18 at 11:59
• @egreg, I've added an example to illustrate a case that I'm not able to solve. That is to make S_0 and S_t to have the same box width. Of course, then $S_t$ will be not right aligned anymore but 'S' will stay in the same horizontal position as before. – gvgramazio Dec 13 '18 at 12:09
• I remember that in the past I've found commands to handle the width and height of boxes (without altering the content) but I do not remember which they are. – gvgramazio Dec 13 '18 at 12:10
• @Marijn Provided. – gvgramazio Dec 13 '18 at 12:10

Are you looking for this?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\newsavebox{\longtext}\sbox{\longtext}{$S_{000}$}
\node [align=left] (first) {long text\\ $S_{000}$};
\node [below=1em of first, align=left] (second) {long text\\ $S_{ttt}$};
\node [below=1em of second, align=right] (third) {long text\\ $S_{000}$};
\node [below=1em of third, align=right] (fourth)
{long text\\ \makebox[\wd\longtext][l]{$S_{ttt}$}};
\end{tikzpicture}
\end{document}


What I have done here is putting the longer text into a "box", called \longtext, and the I typeset the shorter text with \makebox$width$$position${shorter text}: this creates a box with the requested width, left-aligned (for the l), containing the shorter text. As width I used \wd\longtext, which is the length of the box I created before.

• I think that it is exactly what I was looking for. Could you add a bit of explanation of the commands used and why you used them? – gvgramazio Dec 13 '18 at 14:47
• Also, this solution could be applied in the case of vertical space? Like having long text\\ $S_0$ and then long text\\ $S_0$\\ long text and making $S_0$ appearing in the same place? – gvgramazio Dec 13 '18 at 14:49
• I extended a bit the answer as per your first comment; reading the second one I am suspecting maybe you need something similar to a TikZ matrix? texample.net/tikz/examples/feature/matrices – Rmano Dec 13 '18 at 16:01