1

I have always saved my figures generated in matplotlib as .png, .pdf and .tikz, which can easily be included in the document with \includegraphics.

matplotlib also has the option to generate lower level .pgf code. It would be neat to also be able to input the .pgf code files using \includegraphics. The file seems to be read, but the output is clipped.

Notice that the enviroment in img.pgf is tikzpicture. I changed the enviroment generated by matplotlib from pgfpicture to tikzpicture. Using the original pgfpicture makes tikzscale throw the error "Requested to scale unscaleable graphic"

Edit: Sorry for the poorly phrased question. MWE below, and rephrased above.

main.tex

\documentclass{article}
\usepackage{graphicx}   %
\usepackage{tikzscale}  %Allows \includegraphics to parse .tikz files and scales according to width
\usepackage{pgf}
\usepackage{tikz}
\usepackage{subcaption}

\begin{document}

\begin{figure}
    \begin{subfigure}[t]{0.3\textwidth}
        \includegraphics[width=\linewidth]{./img.tikz}
        \caption{Tikz figure read by includegraphics and scaled to linewidth by tikzscale}
    \end{subfigure}
\hfill
    \begin{subfigure}[t]{0.3\textwidth}
        \resizebox{\linewidth}{!}{\input{./img.pgf}}       %This works
        \caption{PGF figure read by input and scaled to linewidth by resizebox}
    \end{subfigure}
\hfill
    \begin{subfigure}[t]{0.3\textwidth}
        \includegraphics[width=\linewidth]{./img.pgf} %But I want this
        \caption{PGF figure read by includegraphics and scaled to linewidth by tikzscale}
\end{subfigure}    
\end{figure}


\end{document}

img.pgf

\pgfdeclareradialshading{new}{\pgfqpoint{0bp}{0bp}}{%
 color(0bp)=(blue);
 color(6bp)=(blue!90!black);
 color(20bp)=(black!75!blue);
 color(30bp)=(black!85!blue)}

 \begin{tikzpicture}
    \begin{pgfscope}
        \pgfpathcircle{\pgfpoint{3cm}{0cm}}{1cm}
        \pgfusepath{clip}
        \begin{pgflowlevelscope}{\pgftransformshift{\pgfpoint{3cm}{0cm}}, \pgftransformrotate{45}}
          \pgfpathellipse{\pgfpoint{0cm}{0.5cm}}
                         {\pgfpoint{1.75cm}{0cm}}
                         {\pgfpoint{0cm}{1.55cm}}
          \pgfshadepath{new}{0}
          \pgfusepath{}
        \end{pgflowlevelscope}
      \end{pgfscope}
      {
      \pgftransformshift{\pgfpoint{3cm}{0cm}}
      \pgfset{minimum size=2cm}
      \pgfnode{circle}{center}{pgf}{nodename}{
        \pgfusepath{}}
      }
\end{tikzpicture}

img.tikz

\pgfdeclareradialshading{new}{\pgfqpoint{0bp}{0bp}}{%
 color(0bp)=(blue);
 color(6bp)=(blue!90!black);
 color(20bp)=(black!75!blue);
 color(30bp)=(black!85!blue)}

\begin{tikzpicture}
\begin{scope}
    \clip (-1,0) circle (1cm);
    \draw [fill=black!70] (0, 0) circle (1cm);
    \begin{scope}[transform canvas={xshift=-1cm, rotate=45}]
        \shade [shading=new] (0.00,0.50) ellipse (1.75 and 1.55);
    \end{scope}
    \node (nodename) at (-1, 0) [circle, minimum size = 2cm] {tikz};
\end{scope}
\end{tikzpicture}
4
  • 1
    .pgf isn't a known extension, presumably these are simply Tex files that you input with \input ? that said you could define a .pgf handler for that extension. it would help if you provided a test file on your question and said whether you were using pdftex or xetex or luatex etc as graphics handling is system dependent Feb 12, 2022 at 18:45
  • you seem to imply .tikz works in includegraphcs, that would not work by default, whatever code you are using to make that work would also work for .pgf Feb 12, 2022 at 20:41
  • hmm OK I wasn't aware of the tikzscale package, as far as I can see that covers pgf and tikz, which is why you get something for your last image (rather than simply failing to recognise pgf ar all) if you rename img.pgf to img2.tikz and then use the \includegraphics you still get the same result with just the left sector of the circle. Might need someone with better tikz knowledge than me to debug Feb 13, 2022 at 9:41
  • Yeah, I tried to replace the pgfpicture enviroment with tikzpicture, and change the extension to .tikz. Seems the scaling is messed up. Feb 13, 2022 at 18:05

1 Answer 1

1

You can convert all your TikZ pictures to PDF and load these pictures as PDF. This has second benefit: you need not wait for processing all your (maybe huge) TikZ pictures in every TeX run on your document.

The conversion can be done as follows. Create a separate document, put TikZ picture at separate page of such document and without page numbers and headers. Process such document by TeX and create PDF output. Then do:

pdfcrop output.pdf
pdfseprate output-crop.pdf pic-%d.pdf

Your pictures are saved in pic-1.pdf, pic-2.pdf etc.

1
  • Did what you suggest here for my MSc thesis. Had a separate document to compile tikz with externalize, and a fancy-pancy preamble which recreated the names- and folders from /tikzfiles/ in the externalized /tikzfigs/ folders. In other words, the pdf's had the correct names, and were saved in the correct location, not just as fig-0, fig-1, etc, in a bucket. Switched to .pdf and .png with full .tex backend in matplotlib after that. Now, I just want to see if the workflow can be really smooth. Seems neat to include the low level pgf code generated by matplotlib. Feb 13, 2022 at 7:01

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.