3

Any idea to export this circuitikz to PDF?
I used tikzpicture but it cut part of image on the right. That is probably due to the incompability between tikz and circuitikz.
Note that the PDF size should be the same as the image size, so please don't suggest compile and export the whole PDF page.

\documentclass[]{article}
\usepackage{circuitikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{backgrounds}
\usetikzlibrary{external}
\tikzexternalize 


\begin{document}


\begin{tikzpicture}
\centering
  \begin{circuitikz}
    \draw (0,0) node (N5){}
    to [short,o-](2,0) 
    to[short,*-](4,0)
    to[D](4,2)
    to[D](4,4)
    to[short,-*](2,4) node (N1){}
    to[short,-o](0,4) node (O){}; 
    \draw (4,0) 
    to[short,*-](8,0) 
    to[D](8,2)
    to[D](8,4)
    to[short,-*](4,4);
    \draw (2,0)
    to[Tnpn,n=q2](2,2) node(N2){}
    to[Tnpn,n=q1](2,4);
    \draw (8,0)
    to[short,*-](10,0) node(N4){}
    to[Tnpn,n=q4,mirror](10,2)
    to[Tnpn,n=q3,mirror](10,4)
    to[short,-*](8,4);
    \draw
    (q1.B) to[short,-o](1,3) node[left]{$Q_1$}
    (q2.B) to[short,-o](1,1) node[left]{$Q_2$}
    (q3.B) to[short,-o](11,3) node[right]{$Q_3$}
    (q4.B) to[short,-o](11,1) node[right]{$Q_4$};
    \draw (2,2) to[short,*-*](4,2) to[Telmech=M](8,2) to[short,*-*](10,2) node(N3){};
    % adding current
    \begin{scope}[on background layer, very thick,decoration = {
        markings,
        mark = at position 0.05 with {\arrow{>}}}
    ] 
    \draw[line width = 3pt, red!40, postaction = {decorate}] (O.north)
    -- node[above, red]{$i$} (N1.north east) |- (N3.north west) |-
    (N5.north); 
    \end{scope}
    % adding voltage
    \draw[line width = 3pt, red!40, ->] (N5) to[out = 150, in = 210]
    (O) node[left, red]{$V$};
  \end{circuitikz}
\end{tikzpicture}

\end{document}
2
  • 1
    Circutikz is a tikzpicture, so you are putting a tikzpicture inside another tikzpicture and they do not nest. Also, \centering inside a tikzpicture does nothing. Commented Dec 7, 2022 at 14:30
  • @JohnKormylo I see, thank you.
    – internet
    Commented Dec 7, 2022 at 21:40

2 Answers 2

9

IMHO the problem is the resulting bounding box of the tikzpicture not an incompatibility. You could fix this using \useasboundingbox:

\documentclass{article}
\usepackage{circuitikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{backgrounds}
\usetikzlibrary{external}
\tikzexternalize 

\begin{document}

\begin{tikzpicture}% circuitikz is also a tikzpicture
%  \begin{circuitikz}% but only tikzpicture is externalized.
    \draw (0,0) node (N5){}
    to [short,o-](2,0) 
    to[short,*-](4,0)
    to[D](4,2)
    to[D](4,4)
    to[short,-*](2,4) node (N1){}
    to[short,-o](0,4) node (O){}; 
    \draw (4,0) 
    to[short,*-](8,0) 
    to[D](8,2)
    to[D](8,4)
    to[short,-*](4,4);
    \draw (2,0)
    to[Tnpn,n=q2](2,2) node(N2){}
    to[Tnpn,n=q1](2,4);
    \draw (8,0)
    to[short,*-](10,0) node(N4){}
    to[Tnpn,n=q4,mirror](10,2)
    to[Tnpn,n=q3,mirror](10,4)
    to[short,-*](8,4);
    \draw
    (q1.B) to[short,-o](1,3) node[left]{$Q_1$}
    (q2.B) to[short,-o](1,1) node[left]{$Q_2$}
    (q3.B) to[short,-o](11,3) node[right]{$Q_3$}
    (q4.B) to[short,-o](11,1) node[right]{$Q_4$};
    \draw (2,2) to[short,*-*](4,2) to[Telmech=M](8,2) to[short,*-*](10,2) node(N3){};
    % adding current
    \begin{scope}[on background layer, very thick,decoration = {
        markings,
        mark = at position 0.05 with {\arrow{>}}}
    ] 
    \draw[line width = 3pt, red!40, postaction = {decorate}] (O.north)
    -- node[above, red]{$i$} (N1.north east) |- (N3.north west) |-
    (N5.north); 
    \end{scope}
    % adding voltage
    \draw[line width = 3pt, red!40, ->] (N5) to[out = 150, in = 210]
    (O) node[left, red]{$V$};
  \useasboundingbox(0,0) rectangle (12,4);% not needed any longer after commenting circuitikz but sometimes useful
%  \end{circuitikz}
\end{tikzpicture}

\end{document}

However, if you need a PDF with the picture only, a very simple solution would be to use class standalone and correct it using option border:

\documentclass[border={-15mm 3mm 16mm 3mm}]{standalone}
\usepackage{circuitikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{backgrounds}
%\usetikzlibrary{external}
%\tikzexternalize 

\begin{document}

\begin{tikzpicture}
  \begin{circuitikz}
    \draw (0,0) node (N5){}
    to [short,o-](2,0) 
    to[short,*-](4,0)
    to[D](4,2)
    to[D](4,4)
    to[short,-*](2,4) node (N1){}
    to[short,-o](0,4) node (O){}; 
    \draw (4,0) 
    to[short,*-](8,0) 
    to[D](8,2)
    to[D](8,4)
    to[short,-*](4,4);
    \draw (2,0)
    to[Tnpn,n=q2](2,2) node(N2){}
    to[Tnpn,n=q1](2,4);
    \draw (8,0)
    to[short,*-](10,0) node(N4){}
    to[Tnpn,n=q4,mirror](10,2)
    to[Tnpn,n=q3,mirror](10,4)
    to[short,-*](8,4);
    \draw
    (q1.B) to[short,-o](1,3) node[left]{$Q_1$}
    (q2.B) to[short,-o](1,1) node[left]{$Q_2$}
    (q3.B) to[short,-o](11,3) node[right]{$Q_3$}
    (q4.B) to[short,-o](11,1) node[right]{$Q_4$};
    \draw (2,2) to[short,*-*](4,2) to[Telmech=M](8,2) to[short,*-*](10,2) node(N3){};
    % adding current
    \begin{scope}[on background layer, very thick,decoration = {
        markings,
        mark = at position 0.05 with {\arrow{>}}}
    ] 
    \draw[line width = 3pt, red!40, postaction = {decorate}] (O.north)
    -- node[above, red]{$i$} (N1.north east) |- (N3.north west) |-
    (N5.north); 
    \end{scope}
    % adding voltage
    \draw[line width = 3pt, red!40, ->] (N5) to[out = 150, in = 210]
    (O) node[left, red]{$V$};
  \end{circuitikz}
\end{tikzpicture}

\end{document}

result of using standalone

Note: With option tikz class standalone does also provide to generate several PDF pages in the same PDF, one for each tikzpicture. To change the options from page to page, you can use \standaloneconfig, e.g.,

\standaloneconfig{border={3mm 3mm 3mm 3mm}}

before the corresponding tikzpicture.

An even simpler suggestion would be to use \documentclass[landscape]{article} and \pagestyle{empty} and afterwards use pdfcrop to cut of the large white margins from the generated PDF. With

pdfcrop --margins 10pt original.pdf

the resulting original-crop.pdf would be almost the same as shown above.

3
  • What if I want to export many pictures in the same tex file?
    – internet
    Commented Dec 9, 2022 at 10:28
  • @internet Please, if you have an additional question, ask an additional questions instead of using a comment. Short comment: Load standalone with option tikz to make one page per tikzimage. And use \standaloneconfig to change other options, e.g. \standaloneconfig{border={3mm 3mm 3mm 3mm}} to change the margin to 3mm around the next tikzpicuture. See the standalone manual for more information.
    – cabohah
    Commented Dec 12, 2022 at 8:57
  • Sorry about that. I didn't intend to ask for multiple figures when I posted the question. Now I decided to make one file for each figure as people seem to agree that it's better.
    – internet
    Commented Dec 12, 2022 at 9:00
6

Although the proposed way to have an independent PDF proposed by @cabohah is arguably the correct/best way, the main error here is that you are nesting tikzpictures. Instead of

\begin{tikzpicture}
    \begin{circuitikz}
...
    \end{circuitikz}
\end{tikzpicture}

use just

\begin{tikzpicture}
...
\end{tikzpicture}

circuitikz is just an alias for tikzpicture; it works most of the time but not when externalize is involved. This is mentioned in the manual:

enter image description here

(FAQ section). Although I admit it is a bit hidden; I am pondering to change all the \begin{circuitikz}...\end{circuitikz} in the manual...

Also, notice that \centering inside a tikzpicture is doing nothing but harm. Probably you are mixing the concept of figure and tikzpicture?

6
  • This is a nice as well.
    – internet
    Commented Dec 7, 2022 at 21:42
  • What would you recommend to export figures to PDFs? For example, I have three figures in the same tex and I want to export to three PDFs? The answer given by cabohah is nice but now I realize that making each tex file for one figure is too much and hard to keep the style consistent.
    – internet
    Commented Dec 10, 2022 at 7:46
  • Well, you can use tikzexternakize, just call all environment tikzpicture. But a well organized set of independent files is almost unbeatable for flexibility. Just keep the styles in a common file, use a standard skeleton for the files, and with the help of a Makefile you can even automate it (I never tried that myself, though, because I use latexmk to compile and I am not sure/never spent the time to learn how o do it.).
    – Rmano
    Commented Dec 10, 2022 at 8:03
  • Thanks for the recommendation. I'll keep them separately then.
    – internet
    Commented Dec 10, 2022 at 8:06
  • 1
    For small projects, you can just prepare files with just the circuit and include them as source, to avoid complexity. It's true that if you have tens of pictures the compilation can slow down a lot (the circuitikz manual, which is monolithic, needs 4+ minutes on a Ryzhen9 with plenty of RAM...)
    – Rmano
    Commented Dec 10, 2022 at 8:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .