Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
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. –  Martin Scharrer Dec 16 '11 at 13:33
add comment

2 Answers

up vote 11 down vote accepted

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

share|improve this answer
add comment

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}
share|improve this answer
    
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 Dec 18 '11 at 19:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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