## LaTeX forum ⇒ Graphics, Figures & Tables ⇒ Can I do this simpler?

Information and discussion about graphics, figures & tables in LaTeX documents.
Simo219h
Posts: 4
Joined: Thu Oct 08, 2015 11:38 pm

### Can I do this simpler?

Hi,

I am very new to Latex. I have tried to draw a circuit, using package from circuitikz.

What do you guys think? can I do this circuit any simpler?

\documentclass[a4paper 12pt]{article}\usepackage[danish]{babel} \usepackage{circuitikz} \usepackage[utf8]{inputenc} \usepackage{graphicx}    \begin{document}\begin{figure}[h!]\begin{center}\begin{circuitikz}  \draw (0,0) to [R, l=$R_1$, i_>=$i_1$] (2,0)--(3,0)to[R,l=$R_4$,i_>=$i_4$](5,0)--(6,0)to[R,l=$R_8$,i_>=$i_8$](8,0)--(9,0)--(9,-3)--(8,-3)to[R, l=$R_7$, i<^=$i_7$] (6,-3)--(5,-3)to[R,l=$R_5$,i<^=$i_5$](3,-3)--(2,-3)to[R,l=$R_2$,i<^=$i_2$](-1,-3)--(-1,0)--(0,0);\draw (2.5,0)to[R,l_=$R_3$,i>=$i_3$,*-*](2.5,-3);\draw (5.5,0)to[R,l_=$R_6$,i>=$i_6$,*-*](5.5,-3);\draw (9,-1.5)to[short,*-](10,-1.5)--(10,-5)to[battery1, v=$V_0$, i>=$i_t$](-2,-5)--(-2,-1.5)to[short,-*](-1,-1.5)(-1,-1.5)node[anchor=west] {A} (2.5,0)node[anchor=south] {B} (2.5,-3)node[anchor=north] {C}(5.5,0)node[anchor=south] {D} (5.5,-3)node[anchor=north] {E} (9,-1.5)node[anchor=east] {F} (9,-1.5);   \end{circuitikz} \end{center}   \end{figure}   \end{document}
Attachments
Circuit.tex
tex file

Tags:

Stefan Kottwitz
Posts: 9595
Joined: Mon Mar 10, 2008 9:44 pm
Hi Simo,

at first some general tips:

• Options should be separated by a comma, such as here:

\documentclass[a4paper,12pt]{article}

• I would give more options to the figure environment, to allow more places (also floating to bottom and top, for optimal page breaks):

\begin{figure}[htbp!]

• Within the figure environment, I would use \centering instead of \begin{center}...\end{center}, since the center environment is for standalone centered text, that's why it adds vertical space before and after it.

You already wrote the code very compact. When I would do it, I would not try to do it simpler in the meaning of shorter, but simpler to read and to maintain. Such as
• Using more spaces for readability
• Aligning the source code a bit
• Using logical node names instead of coordinates, so you know from where to where you make a connection
• Perhaps use relative positioning instead of absolute coordinates
• In case of a complex circuit, I would use a matrix of nodes environment for easy positioning without any coordinate.

Here's an example which I made for my new LaTeX Cookbook: Electrical circuit. It shows drawing nodes with names in a loop, later I connect them.

And here's a diagram example, which shows what I mean with using a matrix, also from the book: Commutative diagram. A circuit is basically similar to a commutative diagram, just with electrical components instead of arrows.

Btw. case you would like to have LaTeX introductory books with many examples, today and for some remaining days the publisher gives them our for cheaper (50% and 35%), click here for the announcement.

Stefan

Simo219h
Posts: 4
Joined: Thu Oct 08, 2015 11:38 pm
Thank you very much for your feedback. It is much appriciated!

I have a question about the line connections.

Using logical node names instead of coordinates, can i do this with the circuitikz package? It was difficult to do with just the coordinates, and I had to "try me forward" to get the line where I wanted it.

short story; relative positioning instead of absolute coordinates, how do I do this?

As said, I am very new to this. Understand if it is a stupid question.

Thank you

/Simo

Stefan Kottwitz
Posts: 9595
Joined: Mon Mar 10, 2008 9:44 pm
Hi Simo,

sure, that's possible, and don't worry about posting questions. I'm just a bit busy but will post suggestions later.

Stefan

Simo219h
Posts: 4
Joined: Thu Oct 08, 2015 11:38 pm
Hi @Stefan_K,

Do you have a suggestion for me?

/Simo

Stefan Kottwitz
Posts: 9595
Joined: Mon Mar 10, 2008 9:44 pm
Hi Simo,

here's a version which used named coordinates. It can be easier for understanding the drawing process. At first, they can be places via "at", here I just used a for loop for calculating them.
A matrix is usually nice for positioning, as in my commutative diagram example, as another way.

Then there's a "below of" and "right of" and such syntax for positioning, but here it would make the code a bit longer. Or one could use so called chains with TikZ if there are longer paths with nodes to draw. Just to mention some ideas, without really making code.

Let's for now see an example with names for positions.

\documentclass{article}\usepackage{circuitikz} \usetikzlibrary{calc}\begin{document}\begin{circuitikz}    \foreach \i in {0,...,3} {     \coordinate (top\i)    at ($3*(\i,0)$);     \coordinate (bottom\i) at ($3*(\i,-1)$);   }   \draw (top0) to [R, l=$R_1$, i_>=$i_1$] (top1)                to [R, l=$R_4$, i_>=$i_4$] (top2)                to [R, l=$R_8$, i_>=$i_8$] (top3)                to                         (bottom3)                to [R, l=$R_7$, i<^=$i_7$] (bottom2)                to [R, l=$R_5$, i<^=$i_5$] (bottom1)                to [R, l=$R_2$, i<^=$i_2$] (bottom0)                to                         (top0);\end{circuitikz}\end{document}

circuit.png (12.06 KiB) Viewed 3435 times

Stefan