# Horizontally-aligning text of different sizes

I would like to left-align two nodes which contain text of different size such that the exact left of the small text is aligned to the exact left of the large text. What I'm finding is that there is a small but noticeable gap between them:

What I would like is something like this, which was obtained by opening the PDF in Inkscape and using the align tool:

A small difference, certainly; but I am particular. Is there any better way of aligning the text as such than doing it manually?

The above example was generated by the following code:

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\usetikzlibrary{positioning}

\node[anchor=text] (largetext) {
\Huge Large text.};
\node[below=.4cm of largetext.text, anchor=text] (smalltext) {
\large small text.};

\draw[red] (largetext.text) ++ (0,.7) -- (smalltext.text);
\end{tikzpicture}

\end{document}

• The issue, I think, is the blank sidebearing of the letters (~1pt at normal text sizes). At larger font sizes, that sidebearing grows in size, and thus the misalignment occurs. Not sure if there is an easy fix. – Steven B. Segletes Nov 17 '16 at 18:45
• You should refer to one of my questions. This might help you: tex.stackexchange.com/questions/338909/… – MaestroGlanz Nov 17 '16 at 18:57
• @MaestroGlanz nope it's a different issue here it's not possible to see the character sidebearings from within TeX. – David Carlisle Nov 17 '16 at 18:59
• Maybe it is possible to add -1pt * \font@sizeHuge / \font@sizeNormalsize . This pseudo code isn't working, but the idea should be clear. – MaestroGlanz Nov 17 '16 at 19:01
• @StevenB.Segletes I see, that was my worry. The text will actually be quite large (> 120pt), so the difference is even more noticeable. If it makes any difference, I will probably use system fonts via fontspec. But, on the bright side, there will only be a handful of such instances, and I can manually adjust the text without too much trouble. – jme Nov 17 '16 at 19:04

This is not a satisfactory answer, but rather a framework:

The idea is that TikZ expects users to pass font commands using font = and node font =. Those commands are stored in \tikz@textfont and \tikz@node@textfont for later use. So at least this part can be automatized.

Once the desired offset is calculated, there are two ways to go

• shift the node by simply passing xshift = -offset; this will affect the anchors for future positioning.
• add negative glues; this can be done by redefining \tikz@text@action. In fact, this command is redefined if the user passes text ragged left or any text alignment style.

Now, wrapping everything you did into a style key. If you want different offsets for different initial letters, you can define a style with argument(s).

In the following example, I used twice the kerning between A and V as the offset. I am not saying that this is correct. But \XeTeXglyphbounds might not give you the correct result as well. The reason is the same as why we need overshoot).

\documentclass[tikz]{standalone}
\usepackage{fontspec}
\setmainfont{Futura}

\begin{document}

\makeatletter
\tikzset{
distil kerning/.code={%
\let\oldtempselectfont\pgfmath@selectfont%
\def\pgfmath@selectfont{\tikz@node@textfont\tikz@textfont\oldtempselectfont}
\pgfmathsetmacro\pgfkerningcorrection{width("V")+width("A")-width("VA")}
\xdef\pgfkerningcorrection{\pgfkerningcorrection}
\let\pgfmath@selectfont\oldtempselectfont
},
node correction by kerning/.style={
distil kerning,
xshift=-\pgfkerningcorrection*2
},
ncbk/.style=node correction by kerning
}
\makeatother

\tikz\draw foreach\A[count=\i]in{A,...,G}{(0,\i*\i/7)node[right,font=\fontsize{\i0}{0}\selectfont,ncbk]{\A: text of size \i0.}};
\tikz\draw foreach\A[count=\i]in{A,...,G}{(0,\i*\i/7)node[right,font=\fontsize{\i0}{0}\selectfont     ]{\A: text of size \i0.}};

\end{document}