5

I have a LaTeX file (compiled with pdflatex) with some images in pdf added via the \includegraphics command. The problem is that the images are really big (up to 10MB) and the output file is too large (more than 30MB).

The images have been obtained via the splot command in gnuplot but there are a lot of points in the splot.

I have tried to get the images in other formats (.jpg, .png basically) since the file size is considerably lower and thus the size of my resulting .pdf is also lower. But when I include this files in my LaTeX document the images lose a lot of quality (even though they have OK quality when viewed separately).

How could I get a small pdf file from pdflatex without sacrificing too much quality on the images? I suppose that the problem lies in resizing the image from the pdf-latex but can't solve it. Or in other words, how can I obtain smaller .pdf images (or other format accepted by pdflatex) without losing too much quality when added to the pdf file?

The LaTeX command is \includegraphics[width=7cm]{file.pdf}. Note that I have to resize them to fit 7cm width.

EDIT:

I add 4 files (original images in .pdf, .png and .jpeg and resulting pdf file compiled with pdflatex. Here's the latex code too.

List of files

\documentclass[a4paper,10pt]{article}
\usepackage[utf8x]{inputenc}
\usepackage[pdftex]{graphicx}

\begin{document}

\begin{figure}
\centering
\includegraphics[width=7cm]{numeric.pdf}
\caption{Pdf file.}
\end{figure}

\begin{figure}
\centering
\includegraphics[width=7cm]{numeric.png}
\caption{Png file.}
\end{figure}

\begin{figure}
\centering
\includegraphics[width=7cm]{numeric.jpeg}
\caption{Jpeg file.}
\end{figure}

\end{document}
  • 2
    Welcome to TeX.SX! You may have a look on our starter guide. – Corentin May 20 '13 at 16:59
  • 4
    If you make a png with the exact size as you need it, the quality will be perfectly fine. Just when you start scaling the issues begin – Martin H May 20 '13 at 17:18
  • 3
    When you include raster graphics using pdflatex, even when scaling, there is no loss of information. pdflatex doesn't alter the image in any way. What you're describing is most likely an issue with your PDF viewer. If you print the document, you should find that the quality is the same as in the original image file. – Jake May 20 '13 at 17:35
  • 3
    @dustin: Well, if the quality of your source image is too low for the intended use (viewing on screen or printing), of course pdflatex can't do anything about it, but including the image using pdflatex will not degrade the quality in any way. – Jake May 20 '13 at 17:42
  • The problem is that my .eps file is 20.8MB and when converted to .pdf it is 10.3MB. With other formats (.png or .jpg) the file size is <1MB and looks good on my image viewer. But on my pdf it looks really bad... – gunbl4d3 May 20 '13 at 17:51
  • @dustin: What do you mean with "eps with dpi=1000", by the way? If it's a vector graphic, the dpi doesn't matter, and if it's a raster graphic, why are you using eps (it's a vector format in which you can embed raster images)? – Jake May 20 '13 at 17:51
  • 1
    @gunbl4d3: Could you maybe edit your question to include a screenshot that illustrates what you mean with "looks really bad"? I still think this is just an artefact of how your PDF viewer displays the image. – Jake May 20 '13 at 17:52
  • @dustin: If I run the example code you linked to, setting dpi=100 and dpi=1000 produces exactly identical files (which is to be expected, since all the objects are purely vector based). It would be different if you had raster based objects in there, but then you shouldn't be using format="eps" but format="png". – Jake May 20 '13 at 18:13
  • @Jake: See edit section. – gunbl4d3 May 20 '13 at 18:16
  • 1
    @gunbl4d3: Yes, this is completely the fault of the PDF viewer: When I open your document in Acrobat Reader and zoom to the right zoom level (66.5%, in my case), the picture is pixel perfect the same as when I open the image file in an image viewer. At any other zoom level the picture is either blurry (if image smoothing is switched on) or it has artifacts (if image smoothing is switched off). I would recommend you generate the image as a PNG with a much higher resolution and using the pngcairo terminal to get native antialiasing. – Jake May 20 '13 at 18:35
  • 1
    @Jake: Ok. I'm already using pngcairo terminal (I think). How do I select the proper resolution on Gnuplot? (if you don't know I'll google it) and which resolution do you reccomend? Edit: Will this affect my printing resolution? And why does it happen? – gunbl4d3 May 20 '13 at 18:39
  • Have you tried to compress the PDF, document PDF after it has been created by pdflatex? – Daniel May 20 '13 at 18:44
  • @Daniel: yes, the file is slightly bigger after the compression detailed in this page (35.2MB->40.6MB) – gunbl4d3 May 20 '13 at 18:56
  • 2
    Depending on how you're going to print it, I would go with 1200dpi for a laser printer, or 600dpi if it's an inkjet printer. So if you need your final image to be 8cm by 4cm, that would be 3840 by 1920 pixels, which you could set using set terminal pngcairo size 4000,2500 font ",90" lw 10 (note that there is no option to specify the resolution directly, unfortunately, so you'll need to take care of scaling the line widths and font size manually). – Jake May 20 '13 at 18:56
  • @Jake: Could you verbose on those calculations so that I can perform my own depending on my actual case? In your case 8cm=3.15 inches so at 600dpi I get 1890 pixels not 3840... Also how do I get the font and lw numbers. – gunbl4d3 May 20 '13 at 19:04
  • @gunbl4d3: You've got it. I was assuming 1200dpi, and 3.2 inches, which gives 3800 pixels. – Jake May 20 '13 at 19:07
  • @Jake: Ok. Thought I was understanding something wrong. What about the font ",90" lw 10 part. How do I get the numbers? And also, if I want to keep using .pdf images will they be printed correctly? Is there a way to decrease the resulting pdf size? – gunbl4d3 May 20 '13 at 19:12
  • 1
    @gunbl4d3: For the lw value, you use the scaling factor (plot width divided by 640 (the default plot width), so 5.94 in this example, and for the font size, you multiply the default font size (12) by the scaling factor (71, in this case). Sticking with pdf would be the best option, but the only real way to decrease the file size is to reduce the sampling frequency of your plot (it's hard to say anything more concrete without knowing how you generate your plot). – Jake May 20 '13 at 19:20
  • @Jake: Okay that was all I wanted to know. Thanks to everyone and specially Jake. My plot is the solution of a differential equation, so it is obtained calculating the values of the grid nodes for a certain grid (the actual procedure is irrelevant). Since I need this grid spacing due to some peculiarities of the equation I guess the .pdf way is a dead-end if I want to improve what I already have. I would vote/thank some of the posts here but I don't really know how... – gunbl4d3 May 20 '13 at 19:50
  • 1
    you need 15 reputation points before you can upvote. BTW as Jake said "Could you edit your question to include a screenshot that illustrates what you mean with "looks really bad"? tex.sx hosting is preferable as external links may break over time. Note: As new user without image posting privileges simply include the image as normal and remove the ! in front of it to turn it into a link. A moderator or another user with edit privileges can then reinsert the ! to turn it into an image again. – texenthusiast May 21 '13 at 01:58

1 Answers1

4

I get really good quality saving my files as eps with a dpi setting of 1000 in Python. With this setting, I can zoom in at the highest level and there is no loss of quality with the lines.

I don't know if there is a way for you to set the dpi in gnuplot. If you can't, make the plot in Python or do it in tikz/pgf-plots.

If you want to check the quality, check out this post I have on code review and run the code.

python optimize ode solving

Edit:

Here is a link to download the files they look better then the google doc output (this isn't the same image the code link produces just the first two I could find the fastest).

converted to pdf

eps

Edit 2:

Here is your data in a pdf compiled in latex(an image of it at least). I deleted the first line that said x, t, u from the datagrid file.

Code: Open ipython with the alias ipython --pylab=qt

from mpl_toolkits.mplot3d import axes3d
import numpy as np
import pylab

x, t, u = np.loadtxt('/home/dustin/Documents/RandomPythonCode/datagrid.txt',
                     unpack = True)
x = x.reshape(-1, 701)
t = t.reshape(-1, 701)
u = u.reshape(-1, 701)

fig = pylab.figure()
ax = fig.add_subplot(111, projection = '3d')
cmap = pylab.get_cmap('selection')
ax.plot_surface(x, t, u, cmap = cmap, linewidth = 0)
pylab.savefig('name.eps', format = 'eps')
pylab.show()

Here are selection options I like: Accent, gist_rainbow, jet, and Paired.

enter image description here enter image description here enter image description here enter image description here

To save it in a rotate form, you need: ax.view_init(elev=elevation_angle, azim=azimuthal_angle) and add in the angle changes which the plot will show you on the bottom as you rotate it.

Edit 3:

From my understanding of reading the gnuplot manual on set hidden3d, set hidden3d hides portion of the plot that wouldn't be viewable from certain angles since in real life we can't see through the object. This by default how the plot is viewed in python. You can't see everything and will obtain different views by rotating the plot. Therefore, I don't think there is anything that needs to be set to accomplish this goal.

dustin
  • 18,617
  • 23
  • 99
  • 204
  • How big are your files? My problem is that eps/pdf files are too big (>10MB). And with other formats, after addding them to the latex file they lose a lot of quality. – gunbl4d3 May 20 '13 at 17:29
  • yes, your quality is high enough, but it does not solve my problem. I believe my problem lies when I add the files to the latex file (appart from huge eps/pdf files). – gunbl4d3 May 20 '13 at 18:28
  • they are 3d-plots made using a grid for a specific differential equation. I don't think it will improve the size unless I decrease the spacing of the grid, which I cannot do due to some pecualiarities of the equation. – gunbl4d3 May 20 '13 at 19:52
  • I have to plot the following file I produced using a C program: file – gunbl4d3 May 20 '13 at 20:02
  • I have actually tried tikz but it just reports an error ! TeX capacity exceeded, sorry [main memory size=3000000] so I guess implementing it in tikz would not be the best choice. As for Phyton I am just starting to learn this language so I don't know if it would improve anything. – gunbl4d3 May 20 '13 at 20:28
  • I would be interested in the eps/pdf file with the image, not the latex compiled one. And better if a degradate in colour like in my own plots could be made. If I could have the code used it would be nice (since I don't know much Phyton yet). Seems you got a better size than I did. – gunbl4d3 May 20 '13 at 21:50
  • get the following error: Traceback (most recent call last): File "python.py", line 10, in <module> ax = fig.add_subplot(111, projection = '3d') File "/usr/lib/pymodules/python2.6/matplotlib/figure.py", line 677, in add_subplot projection_class = get_projection_class(projection) File "/usr/lib/pymodules/python2.6/matplotlib/projections/__init__.py", line 61, in get_projection_class raise ValueError("Unknown projection '%s'" % projection) ValueError: Unknown projection '3d' – gunbl4d3 May 20 '13 at 22:15
  • I don't have iPython. I am running from the UNIX shell on a Debian distribution. – gunbl4d3 May 20 '13 at 22:22
  • I've added import matplotlib just below import pylaband still the same output – gunbl4d3 May 20 '13 at 22:27
  • sudo apt-get install python-matplotlib --upgrade instead. and all are updated. still same error. – gunbl4d3 May 20 '13 at 22:33
  • i'm on python 2.6.6 – gunbl4d3 May 20 '13 at 22:35
  • Seems that matplotlib version 0.99 works a bit different. Managed to run it by tweaking a bit your code. Here it is. Tomorrow i'll look at the options to add colour degradation and a legend for colours... Let's see what I can get. – gunbl4d3 May 20 '13 at 22:53
  • 2
    @gunbl4d3 dustin: is it possible to absorb all relevant comments inside Q & A respectively to become a crispy standalone knowledge entity for future visitors and clean comments which are not required. could you provide a gnuplot splot code to try with gnuplottex and have a look at some approaches. – texenthusiast May 21 '13 at 01:24
  • @dustin Some meta threads which might be of help – texenthusiast May 21 '13 at 04:20
  • @texenthusiast help me with what? – dustin May 21 '13 at 04:22
  • @dustin: Is there any option similar to the set hidden3d option on gnuplot? – gunbl4d3 May 21 '13 at 08:49
  • @gunbl4d3 I don't what is that option? I wouldn't say the option exist, but I would say it could be accomplished by programming it in python. I am not sure though until you tell me what set hidden3d does. – dustin May 21 '13 at 14:10
  • @gunbl4d3 Have a look at this datagrid.txt plot using gnuplottex. datagrid.txt is a large scattered grid data, hence dgrid3d in gnuplot might be a better option. But check if it makes some physical sense to you, being a better judge in it. Incase it's fine i will make an answer. – texenthusiast May 21 '13 at 21:03
  • @texenthusiast there is something wrong with your plot in the data gird line 80024 has .7 in the z whereas your whole plot maxs at .58. Line 111345 reaches .8 – dustin May 22 '13 at 01:49
  • @texenthusiast he said it had to do with some DE. I original asked for the ODEs but he only supplied the data points. – dustin May 22 '13 at 02:04