[Tex/LaTex] Typography and style – good choices for font styles in TikZ or pgfplots graphics

best practicespgfplotstikz-pgftypography

When creating graphics or graphs with TikZ and/or pgfplots, one can put several commands to format the text to his likings.

My question is: what are sound possibilities to format the elements in said graphics created with TikZ or pgfplots?

I an old project of mine I manually edited every single character in any math expression in the graphics/graphs to make it sans serif which was quite tiresome. So I am wondering: maybe there are more clever ways to do it? Maybe there are less elaborate format decisions?

MWE picture

enter image description here

MWE code

\documentclass[
a4paper
]{scrartcl}

\usepackage{
newtxtext,
tikz,
amsmath,
subcaption,
capt-of
}
\usepackage[T1]{fontenc}

\begin{document}
\begin{center}
\begin{minipage}{0.3\textwidth}
\begin{tikzpicture}[font=\sffamily\small]
%
\draw[thick] (0,0) rectangle (25mm, 50mm) node[below,xshift=-12.5mm] {All sans.};
%
\filldraw[thick, fill=black] (12.5mm,25mm) coordinate (A) circle (2pt) node[below right=2pt] {A} node[above=10pt, text width=2cm] {Some more text which is needed!};
%
%x
\draw[thick, ->] (A) -- ++(2,0) node[right=2pt] {\(\text{F}_{\text{x}}\)};
%z
\draw[thick, ->] (A) -- ++(0,-1.5) node[below=2pt] {\(\text{F}_{\text{y}}\)};
\end{tikzpicture}
\captionof{figure}{Text Text word.}
\end{minipage}
\hfill
\begin{minipage}{0.3\textwidth}
\begin{tikzpicture}[font=\small]
%
\draw[thick] (0,0) rectangle (25mm, 50mm) node[below,xshift=-12.5mm] {All serif.};
%
\filldraw[thick, fill=black] (12.5mm,25mm) coordinate (A) circle (2pt) node[below right=2pt] {A} node[above=10pt, text width=2cm] {Some more text which is needed!};
%
%x
\draw[thick, ->] (A) -- ++(2,0) node[right=2pt] {\(\text{F}_{\text{x}}\)};
%z
\draw[thick, ->] (A) -- ++(0,-1.5) node[below=2pt] {\(\text{F}_{\text{y}}\)};
\end{tikzpicture}
\captionof{figure}{And yet another.}
\end{minipage}
\hfill
\begin{minipage}{0.3\textwidth}
\begin{tikzpicture}[font=\small]
%
\draw[thick] (0,0) rectangle (25mm, 50mm) node[below,xshift=-12.5mm] {All serif.};
%
\filldraw[thick, fill=black] (12.5mm,25mm) coordinate (A) circle (2pt) node[below right=2pt] {A} node[above=10pt, text width=2cm] {Some more text which is needed!};
%
%x
\draw[thick, ->] (A) -- ++(2,0) node[right=2pt] {\(F_{x}\)};
%z
\draw[thick, ->] (A) -- ++(0,-1.5) node[below=2pt] {\(F_{y}\)};
\end{tikzpicture}
\captionof{figure}{Word word text.}
\end{minipage}
\end{center}
\end{document}

Best Answer

You mentioned that you hand-edited all math to sans text. I don't recommend doing this: it violates the separation of church and state content and style.1 If you want/need sans-serif math, use a sans-serif math font. They are few, far between, and somewhat limited in terms of glyphs/symbols, but they do exist. For simplicity, I have used sansmath here, but better options do exist in some cases. See the answers of these questions for examples of some possibilities:

The "right answer" here depends on your field. You can refer to a discussion in our chat where some thoughts on this topic were shared. Some fields typically present all text and maths in figures matching the serif body text/maths fonts, while others use sans-serif for text in figures while maths fonts are the same as those in the body text. Still others recommend everything (text and maths) to be sans-serif in figures.

The last option is the most dubious in my opinion, because the same equation will look different depending on if it's presented in a figure or in the body. Furthermore, the typeface used for a particular math symbol is overloaded to impart certain meanings to the mathematics. This can be confusing if it changes through the document and might conflict with some convention used to impart meaning.

In any case, which style is "right" for your document, as I said, depends on your field. Look at high-quality, respected journals and books in your area to determine the best choice.


The styling can be done in a way that it can be easily changed later to reflect changing preferences on the part of the author or requirements of working with a new publisher, for example. Here is a simple way to do this:

\documentclass[tikz]{standalone}

\usepackage[EULERGREEK]{sansmath}

\newcommand{\sample}[2][]{%
\begin{tikzpicture}#1
  \node[draw] (A) at (0,0) {Mathematics: $\sum F = m a$};
  \node[draw] (B) at (5,0) {Emphasized text: \emph{text}};
  \draw[->] (A) -- (B) node[midway,above] {$\Phi\phi$};
  \node at (2.5,-1) {#2};
\end{tikzpicture}}

\begin{document}
\sample{Serif text, serif math}

\sample[\sffamily]{Sans text, serif math}

\sample[\sffamily\sansmath]{Sans text, sans math}

\sample{Serif text, serif math}
\end{document}

enter image description here

enter image description here

enter image description here

enter image description here

This was a bit of a toy example just to quickly show the options.


For a real document, I'd set it up using a package like floatrow to make it easy to change for the whole document in the future. For example:

\documentclass{article}

\usepackage{tikz}

\usepackage{floatrow}
\DeclareFloatFont{henry}{\sffamily}
\floatsetup[figure]{font=henry}

\begin{document}
Here is body text.
\begin{figure}[htbp]
\centering
Text in a figure.
\caption{A figure with just text.}
\end{figure}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
  \node[draw] (A) at (0,0) {Mathematics: $\sum F = m a$};
  \node[draw] (B) at (5,0) {Emphasized text: \emph{text}};
  \draw[->] (A) -- (B) node[midway,above] {$\Phi\phi$};
\end{tikzpicture}
\caption{A figure with a \texttt{tikzpicture}.}
\end{figure}
\end{document}

enter image description here

Or, to add sans-serif math without changing the document body's source one bit:

\documentclass{article}

\usepackage{tikz}

\usepackage[EULERGREEK]{sansmath}
\usepackage{floatrow}
\DeclareFloatFont{henry}{\sffamily\sansmath}
\floatsetup[figure]{font=henry}

\begin{document}
Here is body text.
\begin{figure}[htbp]
\centering
Text in a figure.
\caption{A figure with just text.}
\end{figure}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
  \node[draw] (A) at (0,0) {Mathematics: $\sum F = m a$};
  \node[draw] (B) at (5,0) {Emphasized text: \emph{text}};
  \draw[->] (A) -- (B) node[midway,above] {$\Phi\phi$};
\end{tikzpicture}
\caption{A figure with a \texttt{tikzpicture}.}
\end{figure}
\end{document}

enter image description here

Just for completeness, here's one with a pgfplots axis environment:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\usepackage[EULERGREEK]{sansmath}
\usepackage{floatrow}
\DeclareFloatFont{henry}{\sffamily\sansmath}
\floatsetup[figure]{font=henry}

\begin{document}
Here is body text.
\begin{figure}[htbp]
\centering
Text in a figure.
\caption{A figure with just text.}
\end{figure}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
  \node[draw] (A) at (0,0) {Mathematics: $\sum F = m a$};
  \node[draw] (B) at (5,0) {Emphasized text: \emph{text}};
  \draw[->] (A) -- (B) node[midway,above] {$\Phi\phi$};
\end{tikzpicture}
\caption{A figure with a \texttt{tikzpicture}.}
\end{figure}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\begin{axis}[xlabel={Abscissa, $x$}, ylabel={Ordinate, $y$}]
  \addplot {x^2};
\end{axis}
\end{tikzpicture}
\caption{A figure with a \texttt{pgfplots} axis.}
\end{figure}
\end{document}

enter image description here


TL;DR

So, the bottom line: whatever style you decide is best for your work/field, implement it in a way that separates content from style. This way, you can be flexible if you change your mind or need to work with a person/publisher with different ideas about the style sometime in the future.

The floatrow package provides good ways to make this a reality, or you can roll your own patches of the environments if you can't use floatrow for some reason. Also note that some classes may provide this capability built-in. If this is the case, use the classes built-in facilities for better compatibility. In the case of the KOMA-Script classes, no built-in facility is provided, but floatrow is modified for compatibility with some other tools provided by the class. So styling the floats with floatrow still works as expected:

\documentclass{scrartcl}

\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\usepackage[EULERGREEK]{sansmath}
\usepackage{floatrow}
\DeclareFloatFont{henry}{\sffamily\sansmath}
\floatsetup[figure]{font=henry}

\begin{document}
Here is body text.
\begin{figure}[htbp]
\centering
Text in a figure.
\caption{A figure with just text.}
\end{figure}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
  \node[draw] (A) at (0,0) {Mathematics: $\sum F = m a$};
  \node[draw] (B) at (5,0) {Emphasized text: \emph{text}};
  \draw[->] (A) -- (B) node[midway,above] {$\Phi\phi$};
\end{tikzpicture}
\caption{A figure with a \texttt{tikzpicture}.}
\end{figure}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\begin{axis}[xlabel={Abscissa, $x$}, ylabel={Ordinate, $y$}]
  \addplot {x^2};
\end{axis}
\end{tikzpicture}
\caption{A figure with a \texttt{pgfplots} axis.}
\end{figure}
\end{document}

enter image description here


1 Yes, I know, we all do it sometimes (me too), but this example is particularly extreme and the impact in one document is enormous.