[Tex/LaTex] Eliminating whitespace around characters within TikZ nodes

spacingtikz-pgf

I want to "glue together" single characters in TikZ by putting each character in a node and placing them next to each other. However even with inner sep=0 and outer sep=0 there remains a space.

The following MWE produces such a case:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\framebox{%
\begin{tikzpicture}[inner sep=0,outer sep=0]
\node (k) at (0,0) {K};
\node[anchor=north west] (n) at (k.north east) {N};
\end{tikzpicture}%
}
\end{document}

The result is as shown in the attachment: a black frame with a space and then inside K and N next to each other with a space between.

enter image description here

I want K and N to stick to each other so that they touch (like cropping them to the black parts of the letters) and then the frame touching K and N from all sides.

Best Answer

There is no general way to solve this: the bounding box of each glyph is dictated by the font itself, and TeX can't change anything about it. (TeX works directly with the bounding box of the glyph with no care for where the "ink" is distributed inside [or outside!] that box.)

The problem is the same with TikZ or just normal running text. The only way to solve it is to use manual kerning adjustments or manual shifts, both of which depend on the font and glyph design.

Some semblance of generality can be obtained by using relative units (em for widths and ex for heights), but even this isn't totally perfect since glyphs may have different relative sizes/shapes at the different point sizes.

An illustrative example, with hairline boxes around the glyphs:

\documentclass{article}
\usepackage{tikz}
\tikzset{every node/.style={inner sep=0,outer sep=0,draw,line width=1sp}}
\setlength{\fboxsep}{0pt}
\setlength{\fboxrule}{1sp}

\begin{document}
\begin{tikzpicture}
\node (k) at (0,0) {K};
\node[anchor=north west] (n) at (k.north east) {N};
\end{tikzpicture}
Ti\emph{k}Z, standard

\fbox{K}\fbox{N}
\TeX, standard

\begin{tikzpicture}
\node (k) at (0,0) {K};
\node[anchor=north west,xshift=-0.089em] (n) at (k.north east) {N};
\end{tikzpicture}
Ti\emph{k}Z, shifted

\fbox{K}\kern-0.089em\fbox{N}
\TeX, kerned
\end{document}

enter image description here