1

I use imagemagick to convert pdf file to image when creating tikz code in latex file. The code is as follows:

\documentclass[preview,border=2pt,2pt]{standalone}
\documentclass[convert={density=300,outext=.png}]{standalone}
...

But 2nd line to invoke imagemagick convert does not work. I need help on this. Thanks.

  • 1
    You can only have one documentclass command, so this will not work. You can however combine the two commands by adding the convert option after the other options in the first line. – Marijn Mar 10 '18 at 20:33
  • 1
    Why do you want to convert a .pdf image file to a bitmap format to incorporate it to … another .pdf file? You can incorporate it as is. – Bernard Mar 10 '18 at 20:47
  • @ Marijn, how do you combine the two commands? You can post it as answer. – Eugene Zhang Mar 10 '18 at 21:11
  • @ Bernard, the reason why I want to convert pdf file to image is explained in https://tex.stackexchange.com/questions/413309/how-do-i-create-image-directly-from-latex – Eugene Zhang Mar 10 '18 at 21:12
  • There are many good reasons for converting a PDF vector image to a raster image. Why not invoke imagemagick directly? On Linux, the command is convert' and on Windows it ismagick convert' followed by the other information (density, filenames, etc.) This is done outside of TeX. –  Mar 10 '18 at 21:20
  • @ RobtAll, I don't want to do it in command line. So I need to add it to latex file to convert to image right after pdf file is created. – Eugene Zhang Mar 10 '18 at 21:27
  • @marmot There are several good reasons, which may or may not pertain to the OP. One reason is that a document with included non-raster PDF may subsequently fail to convert to PDF/X or PDF/A, if that is needed. –  Mar 11 '18 at 02:37
  • @RobtAll I do not understand your comment. (No, your comment is well written, I really don't have the background to digest it.) So I'll be happy to remove my above comment if you ask me to. (All I wanted to express is that I don't feel like delving into this, but I agree that such a comment is kind of pointless. ;-) –  Mar 11 '18 at 02:43
  • @marmot You need not delve into this. The OP can do the graphics externally, so it is not a show-stopper. It is a matter of preference for using a certain method. In any case: When a PDF page is inserted as vector PDF, especially with fonts, then subsequent processing to PDF/X or PDF/A may fail to process the internal details of the included page. I don't know why; I just know it can happen, as I have verified via Adobe Acrobat Pro. But an image has no problem, if you don't mind losing the ability to search for text there. –  Mar 11 '18 at 02:50
  • @RobtAll Thanks a lot, that helps! (My comment was simply the suggestion to declare these things TikZ pictures and then externalize them, e.g. by saying \tikz{\node{whatever}}. But I never really tried to do that.) –  Mar 11 '18 at 02:54

2 Answers2

2

The example in the question has two documentclass commands, which is not allowed in LaTeX. You can use a single command with the convert option added to the options of the first line.

Note that the second 2pt option does not do anything, it generates a warning and it can be removed. Note also that you need to run LaTeX with the shell-escape option to allow calling convert. I don't use TeXstudio myself but according to How to invoke latex with the -shell-escape flag in TeXMakerX? you can do it in various ways, one of which is to add a compile directive at the start of the file as in the example below.

MWE:

% !TeX TXS-program:compile = txs:///pdflatex/[--shell-escape]
\documentclass[preview,border=2pt,convert={density=300,outext=.png}]{standalone}
\begin{document}
\Huge converted pdf
\end{document}

Result (note that the squares indicate transparency, they are shown because they are invisible):

enter image description here

Apart from the .png the .pdf is also generated automatically.

Marijn
  • 37,699
  • @ Marijn, I run it and only a text file is created, not png file – Eugene Zhang Mar 10 '18 at 22:31
  • Text file is for pdf file. So there is no image file created, probably because it can not find convert. – Eugene Zhang Mar 11 '18 at 00:23
  • @MathWizard Or you didn't use shell escape. What does the error say? If it can't find convert, it will say so. – cfr Mar 11 '18 at 04:00
  • I run it in TexStudio, and so I don't know the error – Eugene Zhang Mar 11 '18 at 05:14
  • @MathWizard in TeXstudio errors are shown in the bottom of the screen, see https://sourceforge.net/p/texstudio/bugs/1466/attachment/bugstexstudio.PNG. – Marijn Mar 11 '18 at 15:48
  • @Marijn, there is no error in TexStudio. It says "Process exited normally", but no image created. – Eugene Zhang Mar 11 '18 at 18:21
  • @MathWizard could you check the log file? It probably contains some more information. – Marijn Mar 11 '18 at 20:41
  • I can not find log file either – Eugene Zhang Mar 11 '18 at 23:36
  • @MathWizard the log file is created in the same folder as the pdf. It has the same name as the .tex and .pdf files but then with a .log extension (e.g., in my example above the source is called convpng.tex, the pdf is called convpng.pdf and the log file is called convpng.log). You can open the log file in any text editor, for example Notepad in Windows. It should have some lines containing the word runsystem and convert. If there are errors they will be reported around those lines. – Marijn Mar 12 '18 at 09:34
  • See also https://tex.stackexchange.com/questions/93891/conversion-problem-using-standalone-and-imagemagick. – Marijn Mar 12 '18 at 13:27
  • No, I can not find log file in TexStudio. The log file is created by TexWork, different software. – Eugene Zhang Mar 12 '18 at 17:04
  • I think TexStudio creates a text file which is actually log file. But it is pretty long. How do I post it? – Eugene Zhang Mar 12 '18 at 17:07
1

You could write your TikZ externalisation <filename>.tex as

\providecommand*\classoptions{}
\documentclass[\classoptions]{article}
...

and then call pdflatex twice like

pdflatex "\def\classoptions{preview,border=2pt,2pt}\input{<filename>.tex}"

pdflatex --shell-escape "\def\classoptions{convert={density=300,outext=.png}}\input{<filename>.tex}"

to generate a .pdf and the converted file.

Ruben
  • 13,448
  • @ Ruben, I do not call pdflatex from command line. I create and compile latex in texStudio. – Eugene Zhang Mar 10 '18 at 21:15
  • @MathWizard, TeXStudio does also simply call the command line behind the scenes. I'd say it is worth a shot to try this yourself. An opportunity to learn ;) While this may violate your workflow guidlines (for which I'm sorry) I think I'll leave the answer as it might help other users with a similar issue. – Ruben Mar 10 '18 at 22:38
  • @cfr, is it? My test document actually compiled without it - with the engine calls I wrote down above. – Ruben Mar 11 '18 at 09:28
  • @cfr, you're totally right! For some reason I had still defaulted write18 in TeXMaker on the machine I was testing the intermediate steps... tx!!! – Ruben Mar 12 '18 at 16:17
  • LaTeX Warning: Unused global option(s): [convert]. Did not generate png – Vinod Oct 17 '23 at 01:40
  • @Vinod - does your <filename>.tex use the standalone class? – Ruben Oct 18 '23 at 06:55