szhorvat / MaTeX

LaTeX labels in Mathematica
http://szhorvat.net/mathematica/MaTeX
MIT License
368 stars 44 forks source link

Label Resolution in Plot3D, DensityPlot... Is to Low #14

Closed freeman892 closed 7 years ago

freeman892 commented 7 years ago

Hi,

I am trying to use MaTeX[] for labels in a DensityPlot[], but can not make it work. The problem I have is reproducible with the following code {DensityPlot[{Cos[y], Sin[x]}, {y, 0, \[Pi]}, {x, 0, \[Pi]}, PlotLabel -> "Test"], DensityPlot[{Cos[y], Sin[x]}, {y, 0, \[Pi]}, {x, 0, \[Pi]}, PlotLabel -> MaTeX@"Test"]}. If I try to combine the MaTeX[] result with a Plots like Plot3D[] or DensityPlot[], the result will be almost unreadable. I can make it slightly better by Rasterizing[] the result first, enhancing the Resolution, and exporting the image but even the extreme example will not yield a satisfying result.

{DensityPlot[{Cos[y], Sin[x]}, {y, 0, \[Pi]}, {x, 0, \[Pi]}, PlotLabel -> "Test"], DensityPlot[{Cos[y], Sin[x]}, {y, 0, \[Pi]}, {x, 0, \[Pi]}, PlotLabel -> Rasterize[MaTeX@"Test", RasterSize -> 600, ImageResolution -> 600]]}

Does anyone have any idea how to circumvent this? Please excuse me if I am missing something obvious.

Cheers, freeman

Ps: I am on macOS 10.12.5 with Mathematica 11.1.1.0.

szhorvat commented 7 years ago

I am not sure what you mean when you say that the result is unreadable. MaTeX outputs resolution independent vector graphics. It can be displayed at any size without visible pixelation.

If I evaluate your example, I find the result perfectly readable (OS X, "retina" screen).

If I export it to PDF, and view it with a PDF reader such a Preview, it is still perfectly readable.

Perhaps you are not describing in full the manner in which you are viewing these plots, and that is why I cannot see the problem.

Do keep in mind that MaTeX outputs a set of curves. While these can be rendered at any resolution, the rendering is not optimized specifically for text. Thus at very low resolutions the readability will not be as good as true text. Thus if you absolutely must use a raster output (e.g. PNG or TIFF) instead of a vector one (PDF, EPS), then make sure that you are rasterizing at reasonable print resolutions (at least 300 dpi).

freeman892 commented 7 years ago

Thank you very much, for your comprehensive response. This actually plagues me for some time and I dived in the actual content of the plot output. There the MaTeX output looks perfect. Only after merging plot and label does the rendering add some magic. I appended two graphics to illustrate my problem.

The first is a screenshot which shows how the output is rendered on my screen (also retina) in Mathematica. The left side does not use MaTeX the right side does. 2017_07_21_screenshot

The second shows an example of an exported graphic produced by the following code. plot1 = {Plot[Sin[x], {x, 0, \[Pi]}, ImageSize -> 450, PlotLabel -> MaTeX@"Test\\,x^2"], Plot3D[{Cos[y], Sin[x]}, {y, 0, \[Pi]}, {x, 0, \[Pi]}, ImageSize -> 450, PlotLabel -> MaTeX@"Test\\,x^2"]}; Export[FileNameJoin[{NotebookDirectory[], "test.pdf"}], plot1, ImageResolution -> 400] Both plots use MaTeX to plot the label. test.pdf

szhorvat commented 7 years ago

I can see the problem now. It seems that certain graphics (3D graphics or 2D with gradients) will be exported as bitmaps even when using formats like PDF or EPS. This is reasonable because PDF cannot accurately and space-efficiently represent these as vector graphics.

The unfortunate side effect in this situation is that all elements of the graphics will be rasterized, even those which would look better as vector graphics.

Furthermore, the rasterization is done without anti-aliasing (except for text). Again, I find this quite reasonable when targeting print (a common use case for PDF), but it does not look so good on a screen.

I should note that all this is done by Mathematica, not MaTeX, and I do not know how to control it. There are options for PDF export which control rasterization (you can look them up in the documentation), but I found them unreliable.

What can you do in practice?

I suggest one of two possibilities:

Add the MaTeX label separately, like this: Labeled[graphics, MaTeX[...]]. Only the graphics will be rasterized. This is convenient only for labels outside of the graphics area.

A better solution is to prepare your figures to print size, and rasterize everything with 600 dpi resolution. For example, if the variable g contains the graphics object, then

cm = 72/2.54;
Export["g.pdf", Show[g, ImageSize -> 10 cm], ImageResolution -> 600]

Keep in mind that not all elements of the figure scale with the image size (in particular text does not), so check in advance that Show[g, ImageSize -> 10 cm] looks good.

When you include the output in a document, do not rescale it. Include it at precisely its 10 cm size, to ensure that it's 600 dpi resolution will match that of the printer's.

In LaTeX, don't do

\includegraphics[width=something]{g.pdf}

Use simply

\includegraphics{g.pdf}

and control the size in Mathematica.

If some text is not readable at this resolution, it will not be readable at all in print. That is an indication that you should use bigger fonts.

Using this method of scaling also lets you control the font sizes within the graphics precisely. If you set a font size of 10 pt, it will be exactly 10 pt in print. This way you can easily ensure readability (never go below 6 pt).

Personally, I always prepare figures to size, so that I can match font sizes between the figure and the surrounding text, and so that I can guarantee readability while using space efficiently.

alshehab211 commented 3 years ago

I am facing the same problem, even with axes labels as presented here. Any solution for this till now?