4

I'd like to have a style to make my drawings exactly as tall as a capital letter, in order to draw things in a line of text, even if the drawing command is used in a block of text with a different font or size (say, a footnote or the table of contents). I'd like to avoid metrics like em or ex because their definition relative to capital letters is not consistent across fonts (see What is the local height of a capital letter?).

Getting the height of a capital letter works with \settoheight, but not inside \tikz[<styles>]. I guess that is because the TikZ parser sets the font to nullfont or something like that. Wrapping the whole \tikz command in a macro works, but having an actual TikZ style to achieve the same would be nicer. Can this be done?

Output of the example code

\documentclass{article}
\usepackage{tikz}

\newdimen\myLetterHeight \tikzset{ scale to letter height/.code={% \settoheight{\myLetterHeight}{#1}% \pgfkeysalso{/tikz/x=\myLetterHeight, /tikz/y=\myLetterHeight, baseline=0pt}% }, } \newcommand*{\TikzScaleToLetterHeight}[2][X]{\begingroup% \settoheight{\myLetterHeight}{#1}% \pgfkeysalso{/tikz/x=\myLetterHeight, /tikz/y=\myLetterHeight, /tikz/baseline=0pt}% #2% \endgroup}

\begin{document} WITH KEY: \tikz[scale to letter height=X] \draw rectangle (1,1) node[right] {\the\myLetterHeight};

WITH MACRO: \TikzScaleToLetterHeight[X]{\tikz \draw rectangle (1,1) node[right] {\the\myLetterHeight};}

{\Large Large: \TikzScaleToLetterHeight[X]{\tikz \draw rectangle (1,1) node[right] {\the\myLetterHeight};}} \end{document}

Display Name
  • 46,933
Fritz
  • 6,273
  • 31
  • 55

1 Answers1

6

Try this code for the style definition.

A

Measures the height of a letter following the answer in height of a box using pgf where it explains that, for pgf, height("#1") is the equivalent of \heightof{#1} from the calc package.

\documentclass{article}
\usepackage{tikz}

\newdimen\myLetterHeight

\newlength{\NewLetterHeight}

\tikzset{% letterheight/.code={%
\pgfmathparse{height("#1")} %From https://tex.stackexchange.com/a/353904/161015 \setlength{\NewLetterHeight}{\pgfmathresult pt} \pgfkeysalso{/tikz/x=\the\NewLetterHeight, /tikz/y=\the\NewLetterHeight, baseline=0pt}% }, } \newcommand*{\TikzScaleToLetterHeight}[2][X]{\begingroup% \settoheight{\myLetterHeight}{#1}% \pgfkeysalso{/tikz/x=\myLetterHeight, /tikz/y=\myLetterHeight, /tikz/baseline=0pt}% #2% \endgroup}

\begin{document} WITH KEY: \tikz[letterheight=X] \draw rectangle (1,1) node[right] {\the\NewLetterHeight};

WITH MACRO: \TikzScaleToLetterHeight[X]{\tikz
    \draw rectangle (1,1) node[right] {\the\myLetterHeight};}

{\Large Large: \TikzScaleToLetterHeight[X]{\tikz
        \draw rectangle (1,1) node[right] {\the\myLetterHeight};}}

\end{document}

Best solution. The style definition could be further simplified.

\documentclass{article}
\usepackage{tikz}

\tikzset{ scale to letter height/.style={ x={height("#1")}, y={height("#1")}, baseline=0pt }, }

\begin{document} WITH KEY: \tikz[scale to letter height=X]
\draw rectangle (1,1) node[right] {\pgfmathparse{height("X")}\pgfmathresult pt};
\end{document}

Simon Dispa
  • 39,141
  • Thank you. Looks like I was not thorough enough when trying out how to determine the height of a piece of text... I'd put the neat example at the top of your answer (or remove the complicated example entirely, because with the height() approach it is unnecessarily complex and I would not recommend it as the first solution to anyone coming across this thread in the future). – Fritz Mar 22 '22 at 09:53
  • @Fritz Thank you for your feedback! – Simon Dispa Mar 22 '22 at 12:27