15

I would like to draw a circular TikZ node with + in its center, but font=\ttfamily moves it up. Why? How to solve it?

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
    or/.style = {circle,    
        minimum size=4mm,  
        very thick,  
        draw,  
        font=\ttfamily, 
    },
]
\node[or,font=\normalfont] (a) {+};
\draw[very thin,red] (a.west)--(a.east);

\node[or] (b) at (1,0) {+};
\draw[very thin,red] (b.west)--(b.east);

\end{tikzpicture}
\end{document}

enter image description here

1
  • The official center and the optical center do not need to lie at the same position. Every character has an official bounding box which is used by (La)TeX including packages like TikZ. This bounding box can be larger or smaller then the lines of the character. How the character is actually drawn is part of the fonts which is ignored by (La)TeX, AFAIK. So there is no automatic way to center the + because TeX doesn't know about this two lines which make up the plus. Commented Dec 16, 2011 at 13:33

2 Answers 2

11

By design, the + in Computer Modern has its horizontal bar slightly above what we would consider the visual center; this "fools" TiKZ's algorithm for centering an object on the node coordinates.

\documentclass[border=1]{standalone}

\newcommand{\ttplus}{%
  \raisebox{-.15ex}[\dimexpr\height-.15ex\relax][0pt]{\ttfamily+}}

\usepackage{tikz}
\begin{document}
\hbox{\fboxsep=0pt \fboxrule=.2pt % just to have no sep and lighter rules
\fbox{+}\,a\,\fbox{\ttfamily+}\,\fbox{\ttplus}}
\hbox{\begin{tikzpicture}[
    or/.style = {circle,
        minimum size=4mm,
        very thick,
        draw,
    },
]
\node[or,font=\normalfont] (a) {+};
\draw[very thin,red] (a.west)--(a.east);

\node[or] (b) at (1,0) {\ttplus};
\draw[very thin,red] (b.west)--(b.east);

\end{tikzpicture}}
\end{document}

enter image description here

11

Just to add to egreg's fine answer. Why it moves up? TikZ will center the enclosing box, as is obvious from the image below:

enter image description here

TeX aligns the letters symbols at their baseline in text mode and at a centerline, if they are in maths mode. Kerning is a partial solution as egreg, showed. Having the + in math is a better solution. It is not just a font issue, but also the way TikZ aligns which is closer to mathmode. Better even design a small cross using TikZ and have it placed at the exact  center.

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\def\boxes{\fbox{\fbox{y}\fbox{x}\fbox{+}\fbox{f}}}
\begin{document}
\begin{tikzpicture}[
    or/.style = {circle,    
        minimum size=6mm,  
        very thick,  
        draw,  
        font=\ttfamily, 
    },
]
\node[or,font=\normalfont] (a) {yx\textsuperscript{2}+};
\draw[very thin,red] (a.west)--(a.east);

\node[or] (b) at (1.3,0) {$x^2_3+$};
\draw[very thin,red] (b.west)--(b.east);
\fboxsep0pt\fboxrule0.1pt
\node[or] (c) at (2.5,0) {\fbox{\fbox{y}\fbox{x}\fbox{+}}};
\draw[very thin,red] (c.west)--(c.east);

\end{tikzpicture}

\texttt{\boxes} 
\end{document}
1
  • Thank you also for answering 'why?'. Your proposal of drawing a small cross is nice, I've already used it sometimes but I wanted to know if it was possible to avoid it.
    – Ignasi
    Commented Dec 18, 2011 at 19:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .