gnuplot pdfcairo matches latex font size
Asked Answered
V

1

6

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

Verdha answered 7/6, 2018 at 18:57 Comment(4)
Not really an answer to your question, but if you can want to match latex font sizes (and all other aspects of text rendering) in your figures you can use the cairolatex terminal, which leaves the text rendering to latex.Leucopenia
@Leucopenia Thank you for your suggestion. Because I also use the figures in other places, I need show the text in the figure.Verdha
If you want you can do that with cairolatex as well -- you generate a standalone latex file and then run pdflatex over it, which you can do from within the gnuplot script. For example: set term cairolatex pdf standalone header "\\usepackage{amsmath}" font ",9" size 3.4167in,2in; set outp "test.tex"; plot sin(x); set outp; system "pdflatex test.tex" The advantage is that you get the full range of Latex abilities and consistency with other, Latex-generated text.Leucopenia
@Leucopenia Nice, I don't know this before. Thank you.Verdha
H
6

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.

Humectant answered 8/6, 2018 at 13:50 Comment(2)
Thank you. Finally I understand. Cairo uses 96dpi, and it converts font sizes (in points, pt) to Cairo units (in pixels), e.g., 10pt * (1/72 inch/pt) * (96 pixel/inch) = 13.3 pixel as shown in your answer. However, gnuplot uses 72dpi. So this inconsistency results in the larger font size with factor 4/3.Verdha
A related issue is that the pdf produced by gnuplot+pdfcairo will have a pts size that assumes 72dpi. With some pdf viewers (e.g. mupdf) opening the pdf with the default resolution (96dpi for me) shows the content 75% smaller. With other pdf viewers (e.g. okular), it will show at the right magnification... Check for yourself when using set term pdfcairo size 10 cm, 10 cm. For me, the file has size 283 x 283 pts which corresponds to 10cm x 10cm only if dpi=72 (10cm = 2.54cm/in * 283pts/72dpi). It seems Okular assumes 72dpi, so it opens these pdf at the right size, inflates other pdfs...Fleam

© 2022 - 2024 — McMap. All rights reserved.