4

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.

  • 2
    Use kerning adjustments instead. Remove the frame box and put this \node[draw] (k) at (0,0) {K\!N}; – percusse Feb 7 '15 at 10:16
  • \! gives a syntax error. If I put it as $\!$ then it works but just for this combination of letters, because \! is a negative half-space. I want a method for really getting a letter in "autocropped" shape into a node. – peschü Feb 8 '15 at 11:16
5

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

  • I see, I feared that there is no general way to do this. Thank you! – peschü Mar 9 '15 at 6:07
2

This is just an alternative way of drawing the frame within the TiKZ picture using the fit library, rather than using a \framebox{} or \fbox{}. It shamelessly steals the adjustment required between the two letters from Paul Gessler's answer. If you wish, you can obviously add further adjustments to eliminate the tiny distance from the letters to the left and right sides of the frame. As Paul Gessler explained, however, this would have to be done manually on a case-by-case basis. (For example, the N is slightly further from the right hand line of the frame than the K is from the left hand one.)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}
\begin{document}
    \begin{tikzpicture}[inner sep=0pt, outer sep=0pt]
      \node (k) at (0,0) {K};
      \node[anchor=north west,xshift=-0.089em] (n) at (k.north east) {N};
      \node [fit=(k) (n), draw=gray, ultra thin] {};
    \end{tikzpicture}
\end{document}

fitted frame

0

with xshift

\documentclass{article}
\usepackage{tikz}
\begin{document}
\framebox{%
\begin{tikzpicture}[inner sep=0,outer sep=0]
\node (k) at (0,0) {K};
\node[anchor=north] (n) at (k.north east) {N};
\end{tikzpicture}%
}
\framebox{%
\begin{tikzpicture}[inner sep=0,outer sep=0]
\node (k) at (0,0) {K};
\node[anchor=north,xshift=2pt] (n) at (k.north east) {N};
\end{tikzpicture}%
}
\end{document}
  • 1
    This makes the distance greater and it does not help for the distance to the framebox. Also it is not generic, the 2pt depends on the current font size. – peschü Feb 7 '15 at 10:17
  • You can change xshift=2pt to1pt or what you need and for the box you can use \fboxsep=1pt ou 0pt befor \framebox – touhami Feb 7 '15 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.