# TikZ - set of 1-d point charges

Trying to create a set of alternating (+q,-q etc.) Point charges on a line? Completely new to Tikz.

-
Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – Adam Liter Sep 1 '14 at 4:13

Are you after some thing like this?

\documentclass[tikz, border=3mm]{standalone}
\tikzset{%
plus/.pic={
\node[circle,ball color=orange,inner sep=0pt,minimum width=2.5ex]  {+};
},
minus/.pic={
\node[circle,ball color=green,inner sep=0pt,minimum width=2.5ex]  {-};
}
}

\begin{document}
\begin{tikzpicture}
\foreach \x [evaluate={\a=\x+1}] in {0,2,...,16} {
\pic at (\x,0) {plus};
\pic at (\a,0) {minus};
}
\end{tikzpicture}
\end{document}


-
Thanks Harish, that is exactly what I was looking for! – anonymous Sep 1 '14 at 12:57

Here a MWE using \foreach to plot the charges. You need to set 1 or -1 for your charges. If you need another height than 1 or -1 for the charges you can adjust the value to your needs or just use the yscale-option of tikz.

Hope this helps.

\documentclass[tikz, border=10mm]{standalone}

\begin{document}
\begin{tikzpicture}
\foreach \y [count=\x from 0] in { %
1,-1,-1,1,-1 % set of charges
} {
\draw (\x,0) -- ++(0,\y);
}
\draw [->, >=latex, shorten >= -.5cm, shorten <= -.5cm] (0,0) -- (\x,0);
\end{tikzpicture}
\end{document}


EDIT:

For adjusting the spacing between the x-values there are two different approaches:

Using \foreach with evaluate - You can set \xdiv (x-divisor) to adjust the x-spacing

\documentclass[tikz, border=10mm]{standalone}

\begin{document}
\begin{tikzpicture}
\newcommand{\xdiv}{2}
\foreach \y [count=\i from 0, evaluate=\i as \x using \i/\xdiv] in { %
1,-1,-1,1,-1,1,1 % set of charges
} {
\draw (\x,0) -- ++(0,\y);
}
\draw [->, >=latex, shorten >= -.5cm, shorten <= -.5cm] (0,0) -- (\i/\xdiv,0);
\end{tikzpicture}
\end{document}


Using the xscale-option (here globally on the whole tikzpicture - can also be set with local scopes)

\documentclass[tikz, border=10mm]{standalone}

\begin{document}
\begin{tikzpicture}[xscale=.5]
\foreach \y [count=\x from 0] in { %
1,-1,-1,1,-1,1,1 % set of charges
} {
\draw (\x,0) -- ++(0,\y);
}
\draw [->, >=latex, shorten >= -.5cm, shorten <= -.5cm] (0,0) -- (\x,0);
\end{tikzpicture}
\end{document}


Both examples give this images:

To set up nicer looking charges you can use the arrows-library of tikz:

\documentclass[tikz, border=10mm]{standalone}

\usetikzlibrary{arrows}

\begin{document}
\begin{tikzpicture}
\foreach \y [count=\x from 0] in { %
1,-1,-1,1,-1 % set of charges
} {
\draw [-o] (\x,0) -- ++(0,\y);
}
\draw [->, >=latex, shorten >= -.5cm, shorten <= -.5cm] (0,0) -- (\x,0);
\end{tikzpicture}
\end{document}


Of course you can use other arrow types as stated in the pgfmanual section 16.5 (ver. 3.0). For example [-*] for a filled dot at the line-end.

-