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


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. – hermes Mar 10 '18 at 21:11
  • @ Bernard, the reason why I want to convert pdf file to image is explained in tex.stackexchange.com/questions/413309/… – hermes 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 is magick convert' followed by the other information (density, filenames, etc.) This is done outside of TeX. – user139954 Mar 10 '18 at 21:20

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.


% !TeX TXS-program:compile = txs:///pdflatex/[--shell-escape]
\Huge converted pdf

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.

| improve this answer | |
  • @ Marijn, I run it and only a text file is created, not png file – hermes 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. – hermes Mar 11 '18 at 0: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 4:00
  • I run it in TexStudio, and so I don't know the error – hermes Mar 11 '18 at 5:14
  • @MathWizard in TeXstudio errors are shown in the bottom of the screen, see sourceforge.net/p/texstudio/bugs/1466/attachment/…. – Marijn Mar 11 '18 at 15:48

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


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.

| improve this answer | |
  • @ Ruben, I do not call pdflatex from command line. I create and compile latex in texStudio. – hermes 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 9: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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.