Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gnuplot pdfcairo matches latex font size

I plot a pdf figure using gnuplot 5.0 with pdfcairo terminal, and then insert this figure into a pdf file using latex. However, I found the font size in gunplot is different with the font size in latex, although I set them to be the same.

To make the font size consistent in gnuplot and latex, I have to set the fontscale in gnuplot as 0.75, as shown in the following codes.

gnuplot codes: plot an 'A' in blue (Helvetica, size 12, fontscale 0.75).

set terminal pdfcairo font 'Helvetica,12' size 1cm,1cm fontscale 0.75
set output 'test0.pdf'
set xrange [-1:1]
set yrange [-1:1]
unset xtics
unset ytics
unset border
set label 'A' at 0,0 textcolor 'blue'
p 1/0 notitle

Latex codes: insert the previous figure in original size, and write a black 'A' (Helvetica, size 12) next to the previous 'A'.

\documentclass[12pt]{standalone}
\usepackage{tikz}
\usepackage{helvet}
\usepackage{graphicx}
\begin{document}
\begin{tikzpicture}
\node at (0,0) {\includegraphics{test0.pdf}};
\node at (0.3, -0.025) {\textsf{A}};
\end{tikzpicture}
\end{document}

You can see the final pdf file here. Now we can see these two 'A' are exactly the same size under the gnuplot setting 'fontscale 0.75'. I don't understand why 'fontscale 0.75' should be used in gnuplot, not 'fontscale 1'? Is this a problem of cairo? And is there any elegant way to deal with this font size issue?


Short answer: gnuplot uses 72dpi, while cairo uses 96dpi. (96/72=4/3)

Note: For the same reason, if using pngcairo, 'fontscale 0.75' should also be used to get the right font size.

Alternative solution: cairolatex. @user8153

like image 894
lululxvi Avatar asked Jun 07 '18 18:06

lululxvi


1 Answers

I think this might be due to cairo rendering. In order to set the font size, Gnuplot calls function pango_font_description_set_size with the supplied size, i.e., 12 in your case. However, this is then translated into the cairo units:

the size of the font in points, scaled by PANGO_SCALE. (That is, a size value of 10 * PANGO_SCALE is a 10 point font. The conversion factor between points and device units depends on system configuration and the output device. For screen display, a logical DPI of 96 is common, in which case a 10 point font corresponds to a 10 * (96 / 72) = 13.3 pixel font.

Moreveor, from the documentation of the function pango_cairo_font_map_set_resolution:

/**
 * pango_cairo_font_map_set_resolution:
 * @fontmap: a #PangoCairoFontMap
 * @dpi: the resolution in "dots per inch". (Physical inches aren't actually
 *   involved; the terminology is conventional.)
 *
 * Sets the resolution for the fontmap. This is a scale factor between
 * points specified in a #PangoFontDescription and Cairo units. The
 * default value is 96, meaning that a 10 point font will be 13
 * units high. (10 * 96. / 72. = 13.3).
 *
 * Since: 1.10
 **/

So in summary, it seems that the supplied size is then for the purposes of the rendering multiplied by 96/72 = 4/3 which effectively yields a font of size 16 instead of 12. The scaling factor of 0.75 which you applied would then revert this.

like image 165
ewcz Avatar answered Nov 01 '22 10:11

ewcz