2

Yuck. Presence/absence of descender characters seems to shift the baseline of my text. How do I align the text properly?

enter image description here

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\def\h{0.55cm} 
\def\w{1.5cm}
\tikzset{
  myboxshape/.style={rectangle, outer sep=0, minimum width = \w, minimum height = \h, inner sep=0},
  mybox/.style={myboxshape, very thin, draw},
}
\begin{document}
\begin{tikzpicture}
\matrix[]
{
 \node[mybox] {Vertical}; & \node[mybox] {grungy};  \\
};
\end{tikzpicture}
\end{document}
  • Add text depth=0.25ex, say. – user194703 Feb 21 at 19:35
1

enter image description here

I would define node's style in matrix option. For desired vertical align I would in node's style define node anchor, text depth and text height:

\documentclass[tikz, border=1mm]{standalone}
\usetikzlibrary{matrix}
\def\h{5.5mm}
\def\w{15mm}

\begin{document}
\begin{tikzpicture}

\matrix (m) [matrix of nodes,
             nodes={draw, very thin, inner sep=0pt,
             minimum width=\w, minimum height=\h, 
             text height=2ex, text depth=0.75ex, anchor=base}, % <---
             column sep=-0.5\pgflinewidth,
             row sep=0pt
             ]
{
Vertical    &   grungy  \\
};
\end{tikzpicture}
\end{document}
| improve this answer | |
  • ah.... text height and depth are the things I was missing. Thanks! – Jason S Feb 21 at 19:50
1

E.g. adding text depth=0.25ex,text height=0.8em yields

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\def\h{0.55cm} 
\def\w{1.5cm}
\tikzset{
  myboxshape/.style={rectangle, outer sep=0, minimum width = \w, minimum height = \h, inner sep=0},
  mybox/.style={myboxshape, very thin, draw,text depth=0.25ex,text height=0.8em},
}
\begin{document}
\begin{tikzpicture}
\matrix
{
 \node[mybox] {Vertical}; & \node[mybox] {grungy};  \\
};
\end{tikzpicture}
\end{document}

enter image description here

Notice that there are many ways to simplify this. They include the use of matrix of nodes, i.e. you load the matrix library but do not really use it.

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[myboxshape/.style={rectangle, outer sep=0, 
    minimum width = \pgfkeysvalueof{/tikz/mydims/w}, 
    minimum height =  \pgfkeysvalueof{/tikz/mydims/h}, inner sep=0},
  mybox/.style={myboxshape, very thin, draw,text depth=0.25ex,text height=0.8em},
  mydims/.cd,h/.initial=0.55cm,w/.initial=1.5cm]
\matrix[matrix of nodes, nodes={mybox}]
{
 Vertical & grungy  \\
};
\end{tikzpicture}
\end{document}
| improve this answer | |
1

Quick hack: by adding an invisible character the same height as V and the same depth as gy but zero width. \vphantom{Vgy}

See: How do I create an invisible character?

screenshot

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\def\h{0.55cm} 
\def\w{1.5cm}
\tikzset{
  myboxshape/.style={rectangle, outer sep=0, minimum width = \w, minimum height = \h, inner sep=0},
  mybox/.style={myboxshape, very thin, draw},
}
\begin{document}
\begin{tikzpicture}
\matrix[]
{
 \node[mybox] {\vphantom{gy}Vertical}; & \node[mybox] {grungy\vphantom{V}};  \\
};
\end{tikzpicture}
\end{document}
| improve this answer | |

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.