LaTeX forum ⇒ Graphics, Figures & TablesCan 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?

Postby Simo219h » Thu Oct 08, 2015 11:44 pm

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?

  1. \documentclass[a4paper 12pt]{article}
  2. \usepackage[danish]{babel}
  3. \usepackage{circuitikz}
  4. \usepackage[utf8]{inputenc}
  5. \usepackage{graphicx}
  6.  
  7.  
  8.  
  9.  
  10. \begin{document}
  11. \begin{figure}[h!]
  12. \begin{center}\begin{circuitikz}
  13.  
  14. \draw (0,0) to [R, l=$R_1$, i_>=$i_1$] (2,0)
  15. --(3,0)to[R,l=$R_4$,i_>=$i_4$](5,0)
  16. --(6,0)to[R,l=$R_8$,i_>=$i_8$](8,0)
  17. --(9,0)--(9,-3)--(8,-3)to[R, l=$R_7$, i<^=$i_7$] (6,-3)
  18. --(5,-3)to[R,l=$R_5$,i<^=$i_5$](3,-3)
  19. --(2,-3)to[R,l=$R_2$,i<^=$i_2$](-1,-3)--(-1,0)--(0,0)
  20. ;
  21. \draw (2.5,0)to[R,l_=$R_3$,i>=$i_3$,*-*](2.5,-3)
  22. ;
  23. \draw (5.5,0)to[R,l_=$R_6$,i>=$i_6$,*-*](5.5,-3)
  24. ;
  25. \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)
  26. (-1,-1.5)node[anchor=west] {A}
  27. (2.5,0)node[anchor=south] {B}
  28. (2.5,-3)node[anchor=north] {C}
  29. (5.5,0)node[anchor=south] {D}
  30. (5.5,-3)node[anchor=north] {E}
  31. (9,-1.5)node[anchor=east] {F} (9,-1.5)
  32. ;
  33.  
  34. \end{circuitikz} \end{center}
  35.  
  36.  
  37.  
  38. \end{figure}
  39.  
  40.  
  41.  
  42. \end{document}
  43.  
  44.  
  45.  
  46.  
  47.  
Attachments
Circuit.tex
tex file
(965 Bytes) Downloaded 89 times

Tags:

User avatar
Stefan Kottwitz
Site Admin
Posts: 9501
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Fri Oct 09, 2015 12:57 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
Site admin

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

Postby Simo219h » Fri Oct 09, 2015 6:41 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

User avatar
Stefan Kottwitz
Site Admin
Posts: 9501
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Sat Oct 10, 2015 2:15 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
Site admin

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

Postby Simo219h » Tue Oct 13, 2015 12:14 pm

Hi @Stefan_K,

Do you have a suggestion for me? :lol:

/Simo

User avatar
Stefan Kottwitz
Site Admin
Posts: 9501
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Tue Oct 13, 2015 12:51 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.

  1. \documentclass{article}
  2. \usepackage{circuitikz}
  3. \usetikzlibrary{calc}
  4. \begin{document}
  5. \begin{circuitikz}
  6. \foreach \i in {0,...,3} {
  7. \coordinate (top\i) at ($3*(\i,0)$);
  8. \coordinate (bottom\i) at ($3*(\i,-1)$);
  9. }
  10. \draw (top0) to [R, l=$R_1$, i_>=$i_1$] (top1)
  11. to [R, l=$R_4$, i_>=$i_4$] (top2)
  12. to [R, l=$R_8$, i_>=$i_8$] (top3)
  13. to (bottom3)
  14. to [R, l=$R_7$, i<^=$i_7$] (bottom2)
  15. to [R, l=$R_5$, i<^=$i_5$] (bottom1)
  16. to [R, l=$R_2$, i<^=$i_2$] (bottom0)
  17. to (top0);
  18. \end{circuitikz}
  19. \end{document}


circuit.png
circuit.png (12.06 KiB) Viewed 3103 times


Stefan
Site admin


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 13 guests