3

(Note: this question is similar to quite a few others on SO (e.g. this one, that one, that other one, ... but I could find none discussing the explicit need for the new symbols to have proper font metrics attached.)

Dear Hive Mind,

I need/want to use LaTeX to include custom symbols in Python (matplotlib) plots.

I initially used metafont to create the symbols, but ended up using tikz instead to get vector quality symbols, and packed them up in a dedicated LaTeX package. This works perfectly from the LaTeX perspective. However, I eventually learned that matplotlib requires proper font metrics for elements to be displayed correctly in the figure text elements.

Hence this question: what would be the proper/cleanest way to create new LaTeX symbols with full font metric ?

Can I still use tikz and add the font metric info to it ? Do I need to create a full vector font instead ? What would be the 2021 best tool to do so ? Is pict2e what I am looking for ?

Edit 23.08.2021: pict2e (as suggested here) does not solve the problem with the font metrics.

Note 23.08.2021: I have started looking closely at FontForge, but have encountered some issues.

For the record, here's a simplified excerpt of my tikz code inside the .dtx file:

%    \begin{macrocode}
\tikzset{my style/.style={line width=0.2ex, radius=1ex, baseline=-0.6ex}}

% \end{macrocode} % % \noindent Next, each symbol is being defined individually. % % \begin{macro}{\firstsymbol} % The first symbol: % \begin{macrocode} \newcommand{\firstsymbol}{% \begin{tikzpicture}[my style] \draw (0, 0) circle; \end{tikzpicture}% }

fpavogt
  • 221
  • what happens if you add some text to the circle, e.g. tiny periods at the bottom left and the top right of the circle? – Ulrike Fischer Aug 17 '21 at 07:39
  • @UlrikeFischer: do you mean around the \tikzpicture structure inside the \newcommand entity ? – fpavogt Aug 17 '21 at 07:57
  • I mean inside the tikzpicture, add some \node{.}. Does matplotlib then show the dot and the circle? – Ulrike Fischer Aug 17 '21 at 08:01
  • No, matplotlib only shows (on-screen) the text of the node (if I save using the pgf backend, it all works fine of course). If I ask the node to have (e.g.) a green circle around it with node[circle, draw=green] {.}, it still only shows the text of the node. – fpavogt Aug 17 '21 at 09:02
  • that sounds as if you will have to create a proper font, e.g. with fontforge. – Ulrike Fischer Aug 17 '21 at 09:05
  • I’ve lembedded a SVG into a rescaled box before, and you could adapt this technique to a TikZ vector image. It’d probably be simpler to scale to \baselineskip. – Davislor Aug 17 '21 at 17:23
  • Or select a scaling factor with \mathchoice. – Davislor Aug 17 '21 at 17:43
  • @Davislor: thanks for the suggestion. But --unless I missed something ?-- I still can't get font metrics (that matplotlib requires to display the tikzpicture) that way. For the scaling, I was already drawing the symbols the the correct text scale using ex units. Did you mean something different ? – fpavogt Aug 18 '21 at 06:35
  • @fpavogt Ah, sorry. You’ve got the LaTeX side working correctly, I see, and you’re asking about a different piece of software. Have you tried putting the symbols into a font, with FontForge? – Davislor Aug 18 '21 at 14:27
  • @Davislor: I have downloaded but not yet tried FontForge. Since the symbols I need are highly parametrizable, tikz was ideal to draw them with no fuss, without having to deal with the whole "font" Universe ... But since I end up needing proper font metrics, I guess there's no by-passing this ... ? – fpavogt Aug 22 '21 at 19:06
  • You might be able to do it as a variable font? I believe fontspec now supports those. – Davislor Aug 22 '21 at 20:31

1 Answers1

1

Two years down the line, and the solution has taken the following form.

Essentially, I draw the symbols using TikZ, then create an OpenType font with the resulting SVGs using FontForge, before bundling it into a LaTeX package.

The resulting package was uploaded to CTAN.

fpavogt
  • 221