# Proper horizontal alignment of text in tikz nodes

I am using XeTeX and tikz to generate a cheat sheet for an unusual keyboard layout (gb + apl). Inspired by this answer, I set up a command to quickly associate the symbols that are bound to each key:

\tikzstyle{abstract}=[
rectangle,
draw=black,
rounded corners,
fill=gray!20,
text=black]

\newcommand{\mykey}[4]{%
\resizebox{1.25cm}{1.25cm}{%
\begin{tikzpicture}
\node [abstract, rectangle split, rectangle split parts=2]
{\textbf{\footnotesize{#2} \hfill \footnotesize{#4}} \nodepart{second}\textbf{\footnotesize{#1} \hfill \footnotesize{#3}}};%
\end{tikzpicture}}}


Then I use this command to generate the whole keyboard, as follows:

\begin{document}
\mykey{a}{A}{⍺}{⍶}
\mykey{s}{S}{⌈}{}
% etc
\end{document}


I am quite pleased with the result:

But there are a couple of small details that I do not really like:

• I would like the two columns within each key to be further apart.
• I would like the keys with three symbols to have the top row aligned to the left (for example, in the key with the Y, the capital Y should be aligned with the lowercase y).

Edit: also, I am not sure that the call to \resizebox is the best option to achieve equal square boxes.

Thanks in advance for the help!

In case somebody is interested, I'm using the font APL333 which I downloaded here

The full code for generating the page above is this:

\documentclass[12pt]{scrartcl}
\usepackage[margin=1cm]{geometry}
\usepackage{fontspec}
\setmainfont{APL333}
\usepackage{tikz}

\tikzstyle{abstract}=[
rectangle,
draw=black,
rounded corners,
fill=gray!20,
text=black]

\newcommand{\mykey}[4]{%
\resizebox{1.25cm}{1.25cm}{%
\begin{tikzpicture}
\node [abstract, rectangle split, rectangle split parts=2]
{\textbf{\footnotesize{#2} \hfill \footnotesize{#4}} \nodepart{second}\textbf{\footnotesize{#1} \hfill \footnotesize{#3}}};%
\end{tikzpicture}}}

\begin{document}

\mykey{}{¬}{⋄}{⌺}
\mykey{1}{!}{¨}{⌶}
\mykey{2}{"}{¯}{⍫}
\mykey{3}{£}{<}{⍒}
\mykey{4}{\}{≤}{⍋} \mykey{5}{\%}{=}{⌽} \mykey{6}{\textasciicircum}{≥}{⍉} \mykey{7}{\&}{>}{⊖} \mykey{8}{*}{≠}{⍟} \mykey{9}{(}{∨}{⍱} \mykey{0}{)}{∧}{⍲} \mykey{-}{\_}{×}{ } \mykey{=}{+}{÷}{⌹} \mykey{q}{Q}{?}{⍰} \mykey{w}{W}{⍵}{⍹} \mykey{e}{E}{∊}{⍷} \mykey{r}{R}{⍴}{⌾} \mykey{t}{T}{\textasciitilde}{⍨} \mykey{y}{Y}{↑}{} \mykey{u}{U}{↓}{} \mykey{i}{I}{⍳}{⍸} \mykey{o}{O}{○}{⍥} \mykey{p}{P}{*}{⍣} \mykey{[}{\{}{←}{⍞} \mykey{]}{\}}{→}{⍬} \mykey{a}{A}{⍺}{⍶} \mykey{s}{S}{⌈}{} \mykey{d}{D}{⌊}{} \mykey{f}{F}{\_}{} \mykey{g}{G}{∇}{⍢} \mykey{h}{H}{∆}{} \mykey{j}{J}{∘}{⍤} \mykey{k}{K}{'}{⌸} \mykey{l}{L}{⎕}{⌷} \mykey{;}{:}{⍎}{≡} \mykey{'}{@}{⍕}{≢} \mykey{\#}{\textasciitilde}{⊢}{⊣} \mykey{\textbackslash}{|}{⊢}{⊣} \mykey{z}{Z}{⊂}{⊆} \mykey{x}{X}{⊃}{} \mykey{c}{C}{∩}{} \mykey{v}{V}{∪}{} \mykey{b}{B}{⊥}{⍭} \mykey{n}{N}{⊤}{⍡} \mykey{m}{M}{|}{∥} \mykey{,}{<}{⍝}{⍪} \mykey{.}{>}{⍀}{⍙} \mykey{/}{?}{⌿}{⍠} \end{document}  ## 1 Answer Perhaps matrix would be useful here, see example in the code below. The problem with using \resizebox like that is that everything will be scaled by slightly different amounts, if the sizes of the symbols are slightly different. So not a good idea here I think. I'd probably just set the text width/height/depth to a large enough values. Compared to the original answer, I used a different ampersand replacement (since \& was one of the symbols in the keys), and wrapped each matrix entry in {}. \documentclass[12pt]{scrartcl} \usepackage[margin=1cm]{geometry} \usepackage{fontspec} \setmainfont{APL333} \usepackage{tikz} \tikzset{abstract/.style={ rectangle, draw=black, rounded corners, fill=gray!20, drop shadow, text=black} } \usetikzlibrary{shadows,shapes.multipart, matrix} \newcommand{\mykey}[4]{% \begin{tikzpicture} \matrix[ abstract, matrix of nodes, column sep=3pt, % <-- modify this to change the column spacing nodes={ font=\footnotesize\bfseries, anchor=base, text width=1.1em, text depth=0.6ex, text height=1.7ex, % these may need fine tuning align=center }, name=m, ampersand replacement=\NewCol, inner sep=0, ] { {#2} \NewCol {#4} \\ {#1} \NewCol {#3} \\ }; \draw [very thin] (m.east) -- (m.west); \end{tikzpicture}} \begin{document} \mykey{}{¬}{⋄}{⌺} \mykey{1}{!}{¨}{⌶} \mykey{2}{"}{¯}{⍫} \mykey{3}{£}{<}{⍒} \mykey{4}{\}{≤}{⍋}
\mykey{5}{\%}{=}{⌽}
\mykey{6}{\textasciicircum}{≥}{⍉}
\mykey{7}{\&}{>}{⊖}
\mykey{8}{*}{≠}{⍟}
\mykey{9}{(}{∨}{⍱}
\mykey{0}{)}{∧}{⍲}
\mykey{-}{\_}{×}{ }
\mykey{=}{+}{÷}{⌹}

\mykey{q}{Q}{?}{⍰}
\mykey{w}{W}{⍵}{⍹}
\mykey{e}{E}{∊}{⍷}
\mykey{r}{R}{⍴}{⌾}
\mykey{t}{T}{\textasciitilde}{⍨}
\mykey{y}{Y}{↑}{}
\mykey{u}{U}{↓}{}
\mykey{i}{I}{⍳}{⍸}
\mykey{o}{O}{○}{⍥}
\mykey{p}{P}{*}{⍣}
\mykey{[}{\{}{←}{⍞}
\mykey{]}{\}}{→}{⍬}

\mykey{a}{A}{⍺}{⍶}
\mykey{s}{S}{⌈}{}
\mykey{d}{D}{⌊}{}
\mykey{f}{F}{\_}{}
\mykey{g}{G}{∇}{⍢}
\mykey{h}{H}{∆}{}
\mykey{j}{J}{∘}{⍤}
\mykey{k}{K}{'}{⌸}
\mykey{l}{L}{⎕}{⌷}
\mykey{;}{:}{⍎}{≡}
\mykey{'}{@}{⍕}{≢}
\mykey{\#}{\textasciitilde}{⊢}{⊣}

\mykey{\textbackslash}{|}{⊢}{⊣}
\mykey{z}{Z}{⊂}{⊆}
\mykey{x}{X}{⊃}{}
\mykey{c}{C}{∩}{}
\mykey{v}{V}{∪}{}
\mykey{b}{B}{⊥}{⍭}
\mykey{n}{N}{⊤}{⍡}
\mykey{m}{M}{|}{∥}
\mykey{,}{<}{⍝}{⍪}
\mykey{.}{>}{⍀}{⍙}
\mykey{/}{?}{⌿}{⍠}

\end{document}

• Thanks for your help! I am receiving an error when I try to run xelatex with your definition of \mykey: ! Argument of \pgfmatrixendrow@skip has an extra }. <inserted text> \par l.83 \mykey{[}{\{}{←}{⍞} ? Runaway argument? \& ← \\ ! Paragraph ended before \pgfmatrixendrow@skip was complete. <to be read again> \par l.83 \mykey{[}{\{}{←}{⍞} Jun 16, 2021 at 20:30
• @J.D. Don't know why that happens, if you can add a complete example to your question, with font setup etc, I can at least test. Jun 16, 2021 at 20:35
• Sure! I just edited the page with my old code for \mykey. I'm afraid that might require that you install the font I linked in my question. Jun 16, 2021 at 20:41
• Since one of your key contain "\&" try ampersand replacement=\1 instead of ampersand replacement=\&. Jun 16, 2021 at 21:08
• @vipa Thanks, I realized the same. Had to wrap each cell in {} in addition though. Jun 16, 2021 at 21:11