0

I have been trying to export a tikzfigure to a standalone .png picture. Searching around the internet I found a good answer in the following command, which I am launching from the Windows Command Prompt:

latexmk mytexfile.tex -shell-escape -f

I get a satisfying output with only a few issues:

  1. The last blue barplot is only half rendered
  2. (most important) the background fill of the "severity" node should have partial opacity (50%?) instead it renders in solid white fill
  3. how to make sure that the overall picture background is transparent and not white?

This is the code I am compiling

\documentclass[preview,border=1mm,convert={density=600,outext=.png}]{standalone}

\usepackage{tikz} \usepackage{color} \usepackage{graphicx}

\usepackage{pgfplots} \pgfplotsset{/pgf/number format/.cd, 1000 sep={}} \pgfplotsset{compat=1.17} \usepackage{listings} \lstset{basicstyle=\ttfamily} \usepackage{tabularx} \newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}} \newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}} \usepackage{comment} \usepackage{textcomp} \usepackage{longtable} %\usepackage{pgf-pie} \usepackage[toc,page]{appendix} \usepackage[bibstyle=numeric,citestyle=numeric,backend=biber]{biblatex} \usetikzlibrary{positioning} \usetikzlibrary{decorations.pathreplacing,calligraphy,backgrounds}

\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{figure}[h!] \centering \begin{tikzpicture} \begin{axis}[area style,width=0.8\textwidth, height=6cm, bar width=10pt,axis x line=bottom,axis y line=left, xlabel={time [months]}, ylabel={SPEI},ymin=-3,ymax=2.5] \addplot+[ybar, color=blue] coordinates {(1,0.15) (2,0.32) (3,1.2) (4,0.8) (11,0.8) (12,0.8) (13,1.2) (14,2.3) (15,0.7) (16,0.5) (19,1.5) (20,1.1)}; \addplot+[ybar, color=red, mark=no] coordinates {(5,-1.2) (6,-1.72) (7,-1.6) (8,-1.9) (9,-2.1) (10, -1.2) (17,-1.1) (18,-1.5) }; \addplot[color=black] coordinates{(0,0) (20,0)}; \end{axis} %\draw[help lines] (0,0) grid (10,4); \draw[<->, black, line width=0.8pt] (1.85,0.3) -- (4.25,0.3); \node [] at (3.05,0.5) {duration}; \draw[<->, black, line width=0.8pt] (1.7,2.4) -- (1.7,1.35); \draw [decorate, decoration = {calligraphic brace}] (1.85,2.5) -- (4.25,2.5); %\draw[dashed,help lines] (2.1,1.1) -- (5.3,1.1); \node [] at (0.9,1.9) {intensity}; \node at (3.0,1.8) [rectangle,fill=white!50, fill opacity=0.5, draw] {severity}; \node [] at (3.0,2.75) {severity}; \end{tikzpicture} \label{fig:spi_car} \end{figure}

\end{document}

And this is the output enter image description here

EDIT1: solved issue #1, it was sufficient to push the x-axis to a length of 21 instead of just 20, then I had to readjust all coordinates

Ingmar
  • 6,690
  • 5
  • 26
  • 47
  • I can not reproduce your problem with pdflatex --file-line-error --synctex=1 --shell-escape filename.tex. – hpekristiansen Aug 02 '22 at 16:07
  • 1
    Do you want xmax=21 to show the full width of the last bar - a separate issue. – hpekristiansen Aug 02 '22 at 16:07
  • @hpekristiansen Do you get the expected output (i.e. 50% white shading in the severity box) with pdflatex? also, will it export to a png anyway?

    EDIT: i tried running pdflatex and it gives the correct output in the TexWorks previewer, however no .png file is saved anywhere by doing this. how to do so?

    – Andrea Galletti Aug 02 '22 at 16:42
  • 1
    You need convert and --shell-escape AND permissions to run it. I can not help with that - I do not use windows or latexmk. I can not see how your problem can be related to latexmk. You could create a Minimal Working Example. -your problem is not really related to much/anything in your code. – hpekristiansen Aug 02 '22 at 16:53
  • 1
    Windows users must have installed ImageMagick and change the options in documentclass as follow: \documentclass[preview,border=1mm,convert={convertexe={magick convert},density=600,outext=.png}]{standalone} – vi pa Aug 03 '22 at 12:40
  • If you already have the correct output in the previewer then you can make a screenshot of the graph and save that as png. – Marijn Aug 04 '22 at 15:37
  • @Marijn I am preparing the picture for publication on scientific papers which sometimes have strict resolution requirements. i used your solution for everyday purposes A LOT, but this time i needed asolid workflow to create pictures of the resolution i needed – Andrea Galletti Aug 04 '22 at 17:42
  • In published papers I usually prefer if the graphs are not (originally) png or similar bitmap formats, because they don't look good when zoomed, you can't search the axis titles or other textual elements, and the fonts often don't match the rest of the paper. If your publisher allows it then you could consider creating a vector-based eps or pdf version of the graph (with the standalone documentclass) instead. – Marijn Aug 05 '22 at 07:54

1 Answers1

2

Thanks to the commenters and some extra help around the internet i found what did the trick for me:

  1. I installed ImageMagick, which in turns requires Ghostscript to be installed, and ran my code with the modification suggested by @vipa

     \documentclass[preview,border=1mm,convert={convertexe={magick convert},density=600,outext=.png}]{standalone}
    
  2. as @hpekristiansen pointed out, I needed -shell-escape to be enabled in my TexWorks IDE. This was easily done by following THIS

  3. This was, sadly, not enough as TexWorks would return an error of this sort

    gs: Interpreter revision (9561) does not match gs_init.ps revision (9533)

meaning that conflicting versions of Ghostscript are found by TexWorks. Indeed, by looking into the TexLive installation folder i noticed that a pre-installed, simplified version of ghostscript is included in TexLive, in a folder called tlgs, under tlpkgs. Removing the tlgs dependency from TexLive preferences did not fix the issue. What finally fixed the issue was to remove the tlgs folder altogether, leaving my GhostScript installation the only one, thus avoiding conflicts. The code now saves a correctly rendered .png when ran by using PdfLaTex, whereas I discarded the latexmk way i was initially working with.

Again, thanks to everybody here and to the GhostScript discord server, bunch of nice pals over there too