1

I created two circuit diagrams and they do not align vertically despite being completely identical. Here is the code.

    \documentclass[12pt,a4paper]{article}
    \usepackage[utf8]{inputenc}
    \usepackage[top=2cm, bottom=2cm, left=2cm, right =2cm]{geometry}
    \usepackage{amsmath,amssymb}
    \usepackage{steinmetz}
    \usepackage{graphicx}
    \usepackage{circuitikz}

    \usepackage{textcomp}
    \usepackage{gensymb}
    \newcommand*{\equal}{=}


    \begin{document} 
    \noindent\textbf{\Large Experiment 2.a}\\[7pt]
    We are given the following circuit:\\
    \begin{tikzpicture}[american voltages]
      \draw 
    (0,4) to[sV, l_=$v_s(t) \equal 2 \cdot \mathrm{sin}(2000\pi \cdot t)$]         (0,0)
       to [short] (8,0)
       to [R, l^=$R_1\equal 1\;\mathrm{k}\ohm$,  f<^=$I_r(t)$, v_<=$V_r(t)$]         (8,4)
       to [pC, l_=$C_1\equal 100\mathrm{nF}$] (5,4)
    (0,4) to[L, l^=$L_1\equal 25\mathrm{mH}$] (5,4);
    \end{tikzpicture}\\[20pt]
    \textbf{1.a Let us find its equivalent circiut in the frequency domain}\\[7pt]
    \begin{math}
    \mathrm{{\bf V_{s}}}= 2 \cdot \mathrm{sin}\left(2000\pi \cdot t\right)= 2 \cdot \mathrm{cos}\left(2000\pi \cdot t - 90\degree \right) = 2\phase{-90\degree} \text{ V}\\[7pt]
    \mathrm{{\bf Z_{L}}}= \mathrm{j}{\omega}L = \mathrm{j}\cdot 2000 \cdot \pi \cdot 25 \cdot 10^{-3} = \mathrm{j}157.07\;\ohm\\[7pt]
    \mathrm{{\bf Z_{C}}}=\dfrac{1}{\mathrm{j}{\omega}C}=\dfrac{1}{\mathrm{j}\cdot\pi\cdot100\cdot10^{-9}}=-\mathrm{j}1591.54\;\ohm\\[7pt]
    \end{math}\\[10pt]
    The frequency domain equivalent of the circuit is as follows:\\[10pt]
    \begin{tikzpicture}[american voltages]
      \draw 
    (0,4) to[sV, l_=$\mathrm{{\bf V_{s}}} \equal 2\phase{-90\degree} \text{ V}$] (0,0)
       to [short] (8,0)
       to [R, l_=$1\;\mathrm{k}\ohm$,  f<^=$I_r(t)$,] (8,4)
       to [pC, l_=$-\mathrm{j}1591.54\;\ohm$] (5,4)
    (0,4) to[L, l^=$\mathrm{j}157.07\;\ohm$] (5,4);
    \end{tikzpicture}\\[20pt]
    \textbf{1.b Let us find the total impedance linked to the voltage source}\\
    $\mathrm{{\bf Z_{T}}}=1000+\mathrm{j}157.07-\mathrm{j}1591.54= \fbox{1000 - 1434.07\;\ohm}$\\[20pt]

    \end{document}
2
  • They have very different widths, especialliy since the labels count. You can use \draw[red] (current bounding box.south west) rectangle (current bounding box.north east); to show the borders. Oct 9, 2018 at 1:52
  • @JohnKormylo Yes you are right. The boxes wrapping them having different sizes yet the circuit diagrams themselves are of the same size. Is it possible to shift the whole box to the right so that the diagrams align? Oct 9, 2018 at 2:00

1 Answer 1

2

The following will align the origins to some predefined distance from the left edge.

\documentclass[12pt,a4paper]{article}
    \usepackage[utf8]{inputenc}
    \usepackage[top=2cm, bottom=2cm, left=2cm, right =2cm]{geometry}
    \usepackage{amsmath,amssymb}
    \usepackage{steinmetz}
    \usepackage{graphicx}
    \usepackage{circuitikz}

    \usepackage{textcomp}
    \usepackage{gensymb}
    \newcommand*{\equal}{=}

    \newsavebox{\tempbox}
    \newlength{\tempdima}
    \newlength{\origin}
    \setlength{\origin}{0.3\textwidth}% distance to tikzpicture origin

    \begin{document} 
    \noindent\textbf{\Large Experiment 2.a}\\[7pt]
    We are given the following circuit:\\
    \savebox\tempbox{\begin{tikzpicture}[american voltages]
      \draw 
    (0,4) to[sV, l_=$v_s(t) \equal 2 \cdot \mathrm{sin}(2000\pi \cdot t)$]         (0,0)
       to [short] (8,0)
       to [R, l^=$R_1\equal 1\;\mathrm{k}\ohm$,  f<^=$I_r(t)$, v_<=$V_r(t)$]         (8,4)
       to [pC, l_=$C_1\equal 100\mathrm{nF}$] (5,4)
    (0,4) to[L, l^=$L_1\equal 25\mathrm{mH}$] (5,4);
    \pgfextractx{\tempdima}{\pgfpointanchor{current bounding box}{west}}% negative offset
    \global\tempdima=\tempdima
    \end{tikzpicture}}%
    \hspace*{\dimexpr \origin+\tempdima}\usebox\tempbox\\[20pt]
    \textbf{1.a Let us find its equivalent circiut in the frequency domain}\\[7pt]
    \begin{math}
    \mathrm{{\bf V_{s}}}= 2 \cdot \mathrm{sin}\left(2000\pi \cdot t\right)= 2 \cdot \mathrm{cos}\left(2000\pi \cdot t - 90\degree \right) = 2\phase{-90\degree} \text{ V}\\[7pt]
    \mathrm{{\bf Z_{L}}}= \mathrm{j}{\omega}L = \mathrm{j}\cdot 2000 \cdot \pi \cdot 25 \cdot 10^{-3} = \mathrm{j}157.07\;\ohm\\[7pt]
    \mathrm{{\bf Z_{C}}}=\dfrac{1}{\mathrm{j}{\omega}C}=\dfrac{1}{\mathrm{j}\cdot\pi\cdot100\cdot10^{-9}}=-\mathrm{j}1591.54\;\ohm\\[7pt]
    \end{math}\\[10pt]
    The frequency domain equivalent of the circuit is as follows:\\[10pt]
    \savebox\tempbox{\begin{tikzpicture}[american voltages]
      \draw 
    (0,4) to[sV, l_=$\mathrm{{\bf V_{s}}} \equal 2\phase{-90\degree} \text{ V}$] (0,0)
       to [short] (8,0)
       to [R, l_=$1\;\mathrm{k}\ohm$,  f<^=$I_r(t)$,] (8,4)
       to [pC, l_=$-\mathrm{j}1591.54\;\ohm$] (5,4)
    (0,4) to[L, l^=$\mathrm{j}157.07\;\ohm$] (5,4);
    \pgfextractx{\tempdima}{\pgfpointanchor{current bounding box}{west}}% negative offset
    \global\tempdima=\tempdima
    \end{tikzpicture}}%
    \hspace*{\dimexpr \origin+\tempdima}\usebox\tempbox\\[20pt]
    \textbf{1.b Let us find the total impedance linked to the voltage source}\\
    $\mathrm{{\bf Z_{T}}}=1000+\mathrm{j}157.07-\mathrm{j}1591.54= \fbox{1000 - 1434.07\;\ohm}$\\[20pt]

    \end{document}

This version saves the values to the aux file, and therefore takes two runs to work. It computes its own common origin location.

\placeorigin should be placed to the left of the tikzpicture. \saveorigin goes inside the tikzpicture just before the end. A \placeorigin without a \saveorigin or vice verses will give bad results (but not crash).

\documentclass[12pt,a4paper]{article}
    \usepackage[utf8]{inputenc}
    \usepackage[top=2cm, bottom=2cm, left=2cm, right =2cm]{geometry}
    \usepackage{amsmath,amssymb}
    \usepackage{steinmetz}
    \usepackage{graphicx}
    \usepackage{circuitikz}

    \usepackage{textcomp}
    \usepackage{gensymb}
    \newcommand*{\equal}{=}

\makeatletter
  \newcounter{origin}
  \newlength{\originmin}
  \AtEndDocument{\originmin=-\originmin
    \write\@auxout{\string\xdef\string\commonorigin{\the\originmin}}}

  \newcommand{\placeorigin}{\bgroup
    \stepcounter{origin}%
    \@ifundefined{commonorigin}{}{\@ifundefined{origin\theorigin}{}
       {\@tempdima=\csname origin\theorigin\endcsname\relax
       \hspace*{\dimexpr \commonorigin+\@tempdima}}}%
    \egroup\ignorespaces}

  \newcommand{\neworigin}[2]% #1 = counter, #2 = value
    {\expandafter\xdef\csname origin#1\endcsname{#2}}

  \newcommand{\saveorigin}{\bgroup
    \pgfextractx{\@tempdima}{\pgfpointanchor{current bounding box}{west}}%
    \ifdim\originmin>\@tempdima
      \global\originmin=\@tempdima
    \fi
    \immediate\write\@auxout{\string\neworigin{\theorigin}{\the\@tempdima}}%
  \egroup}
\makeatother

    \begin{document} 
    \noindent\textbf{\Large Experiment 2.a}\\[7pt]
    We are given the following circuit:\\
    \placeorigin
    \begin{tikzpicture}[american voltages]
      \draw 
    (0,4) to[sV, l_=$v_s(t) \equal 2 \cdot \mathrm{sin}(2000\pi \cdot t)$]         (0,0)
       to [short] (8,0)
       to [R, l^=$R_1\equal 1\;\mathrm{k}\ohm$,  f<^=$I_r(t)$, v_<=$V_r(t)$]         (8,4)
       to [pC, l_=$C_1\equal 100\mathrm{nF}$] (5,4)
    (0,4) to[L, l^=$L_1\equal 25\mathrm{mH}$] (5,4);
    \saveorigin% last inside tikzpicture, after \placeorigin
    \end{tikzpicture}\\[20pt]
    \textbf{1.a Let us find its equivalent circiut in the frequency domain}\\[7pt]
    \begin{math}
    \mathrm{{\bf V_{s}}}= 2 \cdot \mathrm{sin}\left(2000\pi \cdot t\right)= 2 \cdot \mathrm{cos}\left(2000\pi \cdot t - 90\degree \right) = 2\phase{-90\degree} \text{ V}\\[7pt]
    \mathrm{{\bf Z_{L}}}= \mathrm{j}{\omega}L = \mathrm{j}\cdot 2000 \cdot \pi \cdot 25 \cdot 10^{-3} = \mathrm{j}157.07\;\ohm\\[7pt]
    \mathrm{{\bf Z_{C}}}=\dfrac{1}{\mathrm{j}{\omega}C}=\dfrac{1}{\mathrm{j}\cdot\pi\cdot100\cdot10^{-9}}=-\mathrm{j}1591.54\;\ohm\\[7pt]
    \end{math}\\[10pt]
    The frequency domain equivalent of the circuit is as follows:\\[10pt]
    \placeorigin
    \begin{tikzpicture}[american voltages]
      \draw 
    (0,4) to[sV, l_=$\mathrm{{\bf V_{s}}} \equal 2\phase{-90\degree} \text{ V}$] (0,0)
       to [short] (8,0)
       to [R, l_=$1\;\mathrm{k}\ohm$,  f<^=$I_r(t)$,] (8,4)
       to [pC, l_=$-\mathrm{j}1591.54\;\ohm$] (5,4)
    (0,4) to[L, l^=$\mathrm{j}157.07\;\ohm$] (5,4);
    \saveorigin% last inside tikzpicture, after \placeorigin
    \end{tikzpicture}\\[20pt]
    \textbf{1.b Let us find the total impedance linked to the voltage source}\\
    $\mathrm{{\bf Z_{T}}}=1000+\mathrm{j}157.07-\mathrm{j}1591.54= \fbox{1000 - 1434.07\;\ohm}$\\[20pt]

    \end{document}

You must log in to answer this question.

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