5

As per the title, I'm trying to draw an electric dipole while utilizing Cambria Math as my math font, in LuaLaTeX. Here's the MWE of my code:

\documentclass{standalone}

\usepackage{unicode-math}
\setmathfont{Cambria Math}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \draw[gray] (0, 0) -- (4, 0);
    \node[red, draw, circle] at (0, 0) {$+$};
    \node[blue, draw, circle] at (4, 0) {$-$};
\end{tikzpicture}
\end{document}

Which produces the following: Two circles respectively containing a + and a -, but they are misaligned.

As indicated by the gray line, the + and the - are not on the same line. I've tried many things, including different node anchors, setting text height and text depth, et cetera however I've not been able to get them properly aligned.

The same does not happen with the default math font, which you can see if you compile the MWE without the two lines concerning Cambria Math.

Does anybody have any idea how to do this? Thank you all in advance.


EDIT: I've tried out a few of the solutions mentioned in the comments, specifically with CambriaMath as some only seem to work without it, and here are the results of the following MWE (yes, i had fun with the colors):

\documentclass{standalone}

\usepackage{unicode-math}
\setmathfont{Cambria Math}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[every node/.style={draw, circle}]
    \draw[help lines] (-1em, 1em) grid (4cm+1em, -3cm-1em);
    \node[red, minimum width=1em] at (0, 0) {$+$};
    \node[blue, minimum width=1em] at (4, 0) {$-$};
    \node[orange, inner sep=1pt, anchor=mid] at (0, -1) {\strut$+$};
    \node[green, inner sep=1pt, anchor=mid] at (4, -1) {\strut$-$};
    \node[olive, minimum width=2em] at (0, -2) {$+$};
    \node[purple, minimum width=2em] at (4, -2) {$-$};
    \node[cyan] at (0, -3) {$+$};
    \node[magenta] at (4, -3) {$-$\vphantom{$+$}};
\end{tikzpicture}
\end{document}

Produces the following: More methods I've tried.

As can be seen in the image, the only method that seems to work with CambriaMath is the \vphantom{+} one mentioned in the comments below.

4
  • 1
    Hm, a bit strange. Possible work aroun can be \documentclass{standalone} \usepackage{unicode-math} \setmathfont{Cambria Math} \usepackage{tikz} \begin{document} \begin{tikzpicture}[C/.style = {circle, draw=#1}] \draw[gray] (0, 0) -- (4, 0); \node[C=red] at (0, 0) {$+$}; \node[C=blue] at (4, 0) {$-\vphantom{+}$}; \end{tikzpicture} \end{document}
    – Zarko
    Nov 18, 2022 at 17:53
  • 1
    In your edit, the orange and green symbols are also aligned properly, but the \strut adds a bit more depth that in the last case (cyan and magenta symbols), which is why they are a bit shifted upwards in the circle. You can adjust text depth to tackle this. Nov 18, 2022 at 23:49
  • Thank you once more. I'll choose between adjusting text depth and using \vphantom, but both methods seem to be equally valid.
    – PurpleMyst
    Nov 18, 2022 at 23:57
  • 1
    If you want to automatically append \vphantom{$+$} to the nodes without explicitly typing it in, you can create a style and add the option execute at begin node={\vphantom{$+$}}. Nov 19, 2022 at 0:01

1 Answer 1

4

I think that this problem is not especially related to Cambria Math, since it seems to be rooted in the fact, that the bounding boxes of the plus and minus signs are very different and, most importantly, that of the minus sign is especially tight.

Compare for example the bounding boxes for the glyphs for Camrbia Math:

\documentclass{article}
\usepackage[margin=15mm]{geometry}
\usepackage{showcharinbox, anyfontsize}

\usepackage{unicode-math}
\setmathfont{Cambria Math}

\begin{document}
\ShowCharInBox{\fontsize{200}{210}\selectfont$+$}
\vskip 1cm
\ShowCharInBox{\fontsize{200}{210}\selectfont$-$}
\end{document}

enter image description here

The bounding boxes for Computer Modern on the other hand would be:

\documentclass{article}
\usepackage[margin=15mm]{geometry}
\usepackage{showcharinbox, anyfontsize}
    
\begin{document}
\ShowCharInBox{\fontsize{200}{210}\selectfont$+$}
\vskip 1cm
\ShowCharInBox{\fontsize{200}{210}\selectfont$-$}
\end{document}

enter image description here

(Side node: Actually, I would've guessed that the bounding boxes of the plus and minus signs are of the same height and width in Computer Modern, but obviously they're not.)


What to do? You can insert a \strut before or after the plus or minus sign:

\documentclass[border=10pt]{standalone}
\usepackage{unicode-math}
\setmathfont{Cambria Math}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \draw[gray] (0, 0) -- (4, 0);
    \node[red, draw, circle] at (0, 0) {\strut$+$};
    \node[blue, draw, circle] at (4, 0) {\strut$-$};
\end{tikzpicture}
\end{document}

enter image description here

This will increase the padding a bit, but you can regulate this by setting inner sep to something small or even zero. Also, you maybe want to adjust the text depth a bit. With inner sep=1pt and text depth=0.25em, you would get:

\documentclass[border=10pt]{standalone}
\usepackage{unicode-math}
\setmathfont{Cambria Math}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \draw[gray] (0, 0) -- (4, 0);
    \node[red, draw, circle, inner sep=1pt, text depth=0.25em] at (0, 0) {\strut$+$};
    \node[blue, draw, circle, inner sep=1pt, text depth=0.25em] at (4, 0) {\strut$-$};
\end{tikzpicture}
\end{document}

enter image description here

So what is happening is: TikZ makes the nodes as large as needed to fit each glyph, and since the height of the bounding box of the minus sign of Cambria Math is that much smaller than that of the plus sign, the glyph gets shifted upwards. Using \strut, you essentially enter a bounding box of zero width, but of maximum glyph height. Thus (and due to the fact that the width of the plus and the minus sign of Cambria Math), the two nodes become equally large.

Another way would be to insert \vphantom{$+$} as proposed by Zarko in the comments.

(Side note: Entering \strut should also work for Computer Modern as the two glyphs for plus and minus seem not to have the same height either. However, since the width of the bounding boxes of the plus and minus signs in Computer Modern are not the same, it would probably be an even better idea in this case to explicitly the size of the nodes using text width or minimum width.)

7
  • Thank you very much for your detailed response. Let me get this straight: unicode-math fundamentally makes the boxes for the two signs very different, therefore by adding \strut we make sure the two boxes have the same size, even if that makes them a bit bigger. Is this an "issue" or just a design choice of unicode-math?
    – PurpleMyst
    Nov 18, 2022 at 23:20
  • @PurpleMyst Yes. TikZ makes a node as large as needed to fit a glyph, and since the height of the bounding box of the minus sign is that much smaller than that of the plus sign, the glyph gets shifted upwards. Using \strut you essentially enter a bounding box of zero width, but of maximum height. Thus (and due to the fact that the width of the plus and the minus sign using unicode-math are the same), the two nodes become equally large. I don't know whether the small height of the bounding box of the minus sign is a choice of design of the makers of the unicode-math package. Nov 18, 2022 at 23:32
  • Thank you for your further response. I'd appreciate it if you could take a look at my latest edit: With Cambria Math (which I've downloaded from here), everything but \vphantom seems to fail.
    – PurpleMyst
    Nov 18, 2022 at 23:49
  • I think that unicode-math does nothing here. In OpenType fonts, one only sets the left side bearing and the right side bearing. The top and bottom will be inherited from the glyph itself. This is coherent with your screenshot.
    – mickep
    Nov 19, 2022 at 10:09
  • 1
    I agree that your answer is correct. I just wanted to suggest that the vertically tighter boundingbox is probably not a TeX thing, but something that comes with the font format.
    – mickep
    Nov 19, 2022 at 11:18

You must log in to answer this question.

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