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}
Jason S
  • 2,838

3 Answers3

2

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}
AndréC
  • 24,137
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}
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}
Zarko
  • 296,517