Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am a beginner in Latex, trying to use it for an industrial document. I have tried to add a simple electrical diagram using the following code. However it is not easy for me to adjust the location of the diagram in the page. Moreover there are probably better ways or tools. Would someone suggest a more elegant solution?

screenshot

\documentclass{article}
\usepackage[top=2cm, bottom=3cm, left=2cm, right=2 cm]{geometry} %Layout of page


\begin{document}

\begin{picture}(50,50)(-30,0)
\linethickness{1pt}

\put(0,50){\line (1,0){80}}
\put(50,50){\line (0,1){20}}
\put(60,50){\line (0,1){20}}
\put(70,50){\line (0,1){20}}
\put(80,50){\line (0,1){20}}

\put(48,75){1}
\put(58,75){3}
\put(68,75){5}
\put(78,75){7}

\put(0,40){\line (1,0){80,0}}
\put(50,40){\line (0,-1){20}}
\put(60,40){\line (0,-1){20}}
\put(70,40){\line (0,-1){20}}
\put(80,40){\line (0,-1){20}}

\put(48,5){2}
\put(58,5){4}
\put(68,5){6}
\put(78,5){8}

\end{picture}


\end{document}

I have added a short piece of code adding a ground wire. I have in mind to add to he right of the original schematic the same schematic, but with 20 wires and the short ground symbol between the two, slightly below. This placement does not matter too much, I am trying to understand wether all 3 diagrams should be a single block of code, or 3 that I could possibly locate individually...

\documentclass{article}
\begin{document}
\begin{picture}(50,20)(-270,-20) % Begin ground symbol
\linethickness{1pt}
\put(-70,10){\line (0,1){20}}
\put(-80,10){\line (1,0){20}}
\put(-75, 5){\line (1,0){10}}
\put(-72,0){\line (1,0){5}}
\end{picture} end % End ground symbol
\end{document}
share|improve this question
add comment

3 Answers

up vote 8 down vote accepted

I think there are two different questions here and I am not sure which one is that you ask: One is how to draw the diagram efficiently I guess,

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {1,2,3,4} {
        \draw (0.3*\x ,2mm) -- (0.3*\x,9mm) 
        node[above] {\pgfmathparse{2*\x-1}\pgfmathprintnumber{\pgfmathresult}}     
        (0.3*\x,-2mm) -- (0.3*\x,-9mm) 
        node[below] {\pgfmathparse{2*\x}\pgfmathprintnumber{\pgfmathresult}};
}
\draw (1.2cm,-2mm) -- ++(-3cm,0) (1.2cm,2mm) -- ++(-3cm,0);
\end{tikzpicture}
\end{document}

which gives

enter image description here,

the other one is how to place this in a page (which is not so clear as a question). If you can provide more details about the page (is it a presentation or a plain article? do you want to wrap some text around it? Is it just a figure? etc.) that you are designing we can help further.

EDIT I hope I got it right but here it goes:

\begin{tikzpicture}
\begin{scope}%--- I just put the first picture in a scope 
\foreach \x in {1,2,3,4} {
        \draw (0.35*\x ,2mm) -- (0.35*\x,9mm) 
        node[above] {\pgfmathparse{2*\x-1}\pgfmathprintnumber{\pgfmathresult}}     
        (0.35*\x,-2mm) -- (0.35*\x,-9mm) 
        node[below] {\pgfmathparse{2*\x}\pgfmathprintnumber{\pgfmathresult}};
}
\draw (1.4cm,-2mm) -- ++(-3cm,0) (1.4cm,2mm) -- ++(-3cm,0);
\end{scope}

\begin{scope}[xshift=5cm]%--- This moves the contents of this scope to the right
\foreach \x in {1,2,...,10} {
        \draw (0.35*\x ,2mm) -- (0.35*\x,9mm) 
        node[above] {\pgfmathparse{2*\x-1}\pgfmathprintnumber{\pgfmathresult}}     
        (0.35*\x,-2mm) -- (0.35*\x,-9mm) 
        node[below] {\pgfmathparse{2*\x}\pgfmathprintnumber{\pgfmathresult}};
}
\draw (3.5cm,-2mm) -- ++(-5cm,0) (3.5cm,2mm) -- ++(-5cm,0);
\end{scope}

% ground symbol does not require a scope anyway but convenient to place it 
% since I am not sure if I got the idea correctly
\begin{scope}[xshift=2.5cm,yshift=-2cm]
\draw (0,0) -- (0,1) (-0.7,0)--(0.7,0);
\draw (-4.5mm,-3mm) -- (4.5mm,-3mm);
\draw (-2mm,-6mm) -- (2mm,-6mm);
\end{scope}
\end{tikzpicture}

enter image description here

I hope I am somewhere close to what you described in your edit.

share|improve this answer
    
Thanks. I should have stated that I needed to have in the page on the same horiz line a 20 wire circuit and a ground, below. In any case, all the proposals look to me far better than my original solution, and are extremely educational. The foreach option appears to be what I was looking for...Still trying to make the 20 circuit scheme however.\documentclass{article} \begin{document} \begin{picture}(50,20)(-270,-20) \linethickness{1pt} \put(-70,10){\line (0,1){20}} \put(-80,10){\line (1,0){20}} \put(-75, 5){\line (1,0){10}} \put(-72,0){\line (1,0){5}} \end{picture} \end{document} –  Yves Feb 14 '12 at 18:52
    
@Yves Sorry for the question but I could not compile your short code :-). It is best to edit your question and include the new code in a compilable form such that we can have a look. –  percusse Feb 14 '12 at 20:31
    
Sorry. I did not do a good job. I have just followed your advice above... –  Yves Feb 14 '12 at 21:55
    
This looks magnificent ! unfortunately, I will need to spend time to fully understand the solution...Is there an easy way to increase slightly the spacing of all vertical bars, in order to leave a bit more space between the two digit numbers?. The "scope" notion seems to do the magic tricks I was unable to handle properly before... –  Yves Feb 15 '12 at 18:56
    
Sure, all those 0.35 can be made 0.5 and the horizontal lines must be extended by the \draw commands etc. I strongly recommend that you use QTikZ or (KTikZ for Linux) to get a fast update on the changes while you are learning the code. Also go through the manual of PGF/TikZ. It's just wonderful. –  percusse Feb 15 '12 at 19:02
add comment

Admittedly, this is a bit crude but hopefully it points you in the right direction. Basically, this sets up a TikZ to path style that lets you draw the diagram at any position in your TikZ picture. Using the center, flushleft, flushright environments you should be able to put the picture in the right position in a figure (assuming you're using a figure environment ot present the picture).

\documentclass{article}
\usepackage{tikz}
\begin{document}

\tikzset{my diagram/.style={to path={
         (\tikztostart) coordinate (a) -- (\tikztotarget) coordinate (b)
         ($(a)!0.4cm!+90:(b)$) coordinate (a')
         ($(b)!0.4cm!-90:(a)$) coordinate (b')
         \foreach \distance/\label in {0.0cm/1,0.2cm/3,0.4cm/5,0.6cm/7} {
             ($(b)!\distance!(a)$) coordinate (tmp) -- ($(tmp)!0.4cm!-90:(a)$)
             node[allow upside down,anchor=south,pos=1,sloped,rotate=-90] {\label}
         }
         ($(a)!0.2cm!-90:(b)$) coordinate (c)
         ($(b)!0.2cm!+90:(a)$) coordinate (d)
         (c) -- (d)
         ($(c)!0.4cm!-90:(d)$) coordinate (c')
         ($(d)!0.4cm!+90:(c)$) coordinate (d')
         \foreach \distance/\label in {0.0cm/2,0.2cm/4,0.4cm/6,0.6cm/8} {
             ($(d)!\distance!(c)$) coordinate (tmp) -- ($(tmp)!0.4cm!+90:(c)$)
             node[allow upside down,anchor=north,pos=1,sloped,rotate=+90] {\label}
         }}}}
\begin{tikzpicture}
   \draw (0,0) to[my diagram] (1.5,0);
   \draw (2,2) to[my diagram] (3.0,0);
\end{tikzpicture}
\end{document}

two to paths

share|improve this answer
add comment

run it with xelatex or latex->dvips->ps2pdf

\documentclass{article}
\usepackage{pst-node}
\def\Node#1{\rnode{#1}{#1}}

\begin{document} 

\pnode(0,-8mm){A}\hspace*{3cm}\Node{1}~\Node{3}~\Node{5}~\Node{7}
\psforeach{\iA}{1,3,5,7}{\ncangle[angleB=-90,nodesepB=2pt]{A}{\iA}}

\vspace{1.7cm}
\pnode(0,10mm){A}\hspace*{3cm}\Node{2}~\Node{4}~\Node{6}~\Node{8}
\psforeach{\iA}{2,4,6,8}{\ncangle[angleB=90,nodesepB=2pt]{A}{\iA}}
\end{document}

enter image description here

share|improve this answer
    
I am experimenting with this solution, and it is remarkably compact. –  Yves Feb 16 '12 at 19:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.