 # Electric Field of Line Charge in TikZ

This tutorial is about drawing an electric field of an infinite line charge in LaTeX using TikZ package. The aim of this post is to show how to draw an arrow in the middle of a line or at any predefined position and use foreach loop for repeated shapes Special thanks!

Thank you guys for your encouraging feedback, your suggestions and comments on each published post. I have received a request from Sebastian to write a tutorial about drawing standard electromagnetic situations and this post is part of it.

Feel free to contact me, I will be happy to hear from you ❤️!

## 1. Draw simple cylindrical shape in TikZ

The line charge is represented with a cylindrical shape. For geometry simplification, we will draw the cylindrical shape (and different arrows) in an horizontal position then we will rotate it get the final results. A cylinder can be drawn by the mean of 2 straight lines, 2 arcs and and an ellipse as follows: And here is the corresponding LaTeX code:

\documentclass[border=0.2cm]{standalone}

\usepackae[dvipsnames]{xcolor}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

% Cylindrical shape
\draw [top color=NavyBlue,
bottom color=NavyBlue,
middle color=Cyan] (0,-0.5) -- ++(12,0)
arc(-90:90:0.2 and 0.5) -- ++(-12,0)
arc(90:-90:0.2 and 0.5)--cycle;

\draw[fill=cyan!10] (0,0) ellipse(0.2 and 0.5);

\end{tikzpicture}

\end{document}


• We started from the point with coordinates (0,-0.5) and we drew an horizontal straight line of 12cm length. Then, we add an arc that starts from -90 degrees and ends at 90 degrees with x-radius equals to 0.2cm and y-radius equals to 0.5cm. After that, we drew a straight line back (-12cm along the x-axis) using relative coordinates. Finally, we closed the path by an arc that starts from 90 degrees and ends at -90 degrees.
• The previous shape is shaded by defining top, middle and bottom colors. It should be noted that we used predefined colors (dvipsnames)
• We added an ellipse filled with $\verb|cyan!10|$ and has the same size as the drawn arcs (x-radius equals to 0.2cm and y-radius equals to 0.5cm).

We would like to add plus sign to represent positive charges. This can be achieved using a node command and a foreach loop as follows:

\foreach \j in {1,3.5,7,11}
{
\node at (\j,0){$+$};
} The loop variable, named $\verb|\j|$, takes values from the set $\verb|{1,3.5,7,11}|$ which are used to define the x-coordinate, along the x-axis, of each node. Each node draws a plus sign at the defined position.

## 3. Draw the Electric field

The electric field is represented by a set of straight lines labelled with an arrowhead to specify its direction. In this example, we would like to draw a set of 18 arrows: 12 arrows behind the cylindrical shape (has to be drawn first) and 6 arrows above the cylindrical shape (has to be drawn last). Each arrow is drawn by one line code and as we need to repeat this 18 times (different angles and different x coordinates) we will use a nested loop (a loop within a loop): one sets the x-coordinate value (1cm, 6cm and 11cm), the second sets the angle of the arrow.

Here is the final code (without rotation):

\documentclass[border=0.2cm]{standalone}

\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{ decorations.markings}

\begin{document}

\begin{tikzpicture}
[decoration={markings,
mark= at position 0.7 with {\arrow[line width=0.5mm]{latex}}}
]

% Electric Field (arrows of back layer)
\foreach \j in{1,6,11}
{
\begin{scope}[xshift=\j cm]
\foreach \angle in {90,150,210,270}
{
\draw[thick,postaction={decorate}] (\angle:0.4) -- (\angle:2.5);
}
\end{scope}
}

% Cylindrical shape
\draw [top color=NavyBlue,
bottom color=NavyBlue,
middle color=Cyan,
opacity=0.92] (0,-0.5) -- ++(12,0)
arc(-90:90:0.2 and 0.5) -- ++(-12,0)
arc(90:-90:0.2 and 0.5)--cycle;

\draw[fill=cyan!10] (0,0) ellipse(0.2 and 0.5);

% Electric charge
\foreach \j in {1,3.5,7,11}
{
\node at (\j,0){$+$};
}

% Electric Field (arrows of front layer)
\foreach \j in {0.75,5.75,10.75}
{
\begin{scope}[xshift=\j cm]
\foreach \angle in {-30,30}
{
\draw[thick,postaction={decorate}] (\angle:0.5) -- (\angle:2.5);
}
\end{scope}
}

\end{tikzpicture}

\end{document}


• We used method 2 for drawing arrows in the middle of a line (check below for the details).
• We used polar coordinates to draw different arrows, the angle is provided by a foreach loop variable.
• Arrowheads are positioned at 0.7 of the path length.

## 4. Rotating scope in TikZ

Now, it remains to rotate the illustration by 10 degrees. This can be achieved by putting the illustration code inside a scope with the option $\verb|transform canvas={rotate=10}|$

\documentclass[border=0.2cm]{standalone}

\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{ decorations.markings}

\begin{document}

\begin{tikzpicture}
[decoration={markings,
mark= at position 0.7 with {\arrow[line width=0.5mm]{latex}}}
]

% Solve rotation issue
\draw[white] (-2,-3) rectangle (13.5,5.25);

% Rotate the illustration
\begin{scope}[transform canvas={rotate=10}]

% Electric Field (arrows of back layer)
\foreach \j in{1,6,11}
{
\begin{scope}[xshift=\j cm]
\foreach \angle in {90,150,210,270}
{
\draw[thick,postaction={decorate}] (\angle:0.4) -- (\angle:2.5);
}
\end{scope}
}

% Cylindrical shape
\draw [top color=NavyBlue,
bottom color=NavyBlue,
middle color=Cyan,
opacity=0.92] (0,-0.5) -- ++(12,0)
arc(-90:90:0.2 and 0.5) -- ++(-12,0)
arc(90:-90:0.2 and 0.5)--cycle;

\draw[fill=cyan!10] (0,0) ellipse(0.2 and 0.5);

% Electric charge
\foreach \j in {1,3.5,7,11}
{
\node at (\j,0){$+$};
}

% Electric Field (arrows of front layer)
\foreach \j in {0.75,5.75,10.75}
{
\begin{scope}[xshift=\j cm]
\foreach \angle in {-30,30}
{
\draw[thick,postaction={decorate}] (\angle:0.5) -- (\angle:2.5);
}
\end{scope}
}

\end{scope} % end scope of the illustration rotation
\end{tikzpicture}

\end{document}


Remark: To avoid facing issues when we use rotation or scaling with transform canvas, we can add a white rectangle around our illustration. Scaling can be achieved by adding the key $\verb|scale=0.7|$ to the transform canvas: $\verb|transform canvas={rotate=10,scale=0.7}|$   without scaling with 0.7 scaling

Next, we will add details about drawing an arrow in the middle or at specific position of a line with 3 different methods!

## How to draw an arrow in the middle of a line?

There are several methods that can be used to draw an arrow in the middle or at predefined position of a line. In this post, we will highlight three methods and it's up to you to choose one of them.

### Method 1: based on CircuiTikZ

This method benefits from the $\verb|currarrow|$ option provided by CircuiTikZ package, which is used for voltage and current arrows. Here is a simple code with different options:

\documentclass[border=0.2cm]{standalone}
\usepackage{circuitikz}

\begin{document}

\begin{tikzpicture}

\draw (0,0) -- (2,2) node[
currarrow,
pos=0.5,
xscale=-1,
sloped,
scale=2] {};
\end{scope}

\end{tikzpicture}

\end{document}  • $\verb|currarrow|$ is a node option;
• $\verb|pos=0.5|$: sets the position of the arrowhead along the path. It takes values from 0 (start of the path) to 1 (end of the path). We can use also predefined positions: at start, very near start, near start, midway, near end, very near end and at end. Check the next table to get the precise position of each one.
• $\verb|xscale=-1|$: this trick is used to define the arrow direction, you should choose between 1 and -1.
• $\verb|sloped|$: it forces the arrow to follow the path slope at the predefined position.
• $\verb|scale=2|$: it can be used to scale the arrowhead and change its size.

Placing Nodes on a Line or a curve

[pos=0]

[at start]

[pos=0.125]

[very near start]

[pos=0.25]

[near start]

[pos=0.5]

[midway]

[pos=0.75]

[near end]

[pos=0.875]

[very near end]

[pos=1]

[at end]

Remark: These options can be used to position a node content (e.g. arrowhead, label, etc) and not limited to this case. For more details, check Placing Nodes on a Line or Curve Explicitly in the PGFmanual

### Method 2: based on decorations library

Despite the fact that the previous method is easy to implement, it is not flexible specially with curved lines. Moreover, it requires loading a full package for only an arrowhead option that can be achieved with one line code.

This method, I recommend, is based on decorations.markings library, which allows us to add a marking on a path with a specific position. A marking is not limited to an arrow tip, it can be a node or even a whole picture. Furthermore, we can add a marking repeatedly along a path with a specific distance.

Here is an  illustrative example:

\documentclass[border=0.2cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{ decorations.markings}

\begin{document}

\begin{tikzpicture}
[decoration={markings,
mark= at position 0.25 with {\arrow{stealth}},
mark= at position 2cm with {\arrow{stealth}}}
]

\draw [postaction={decorate}] (0,0) -- (2,2);

\end{tikzpicture}

\end{document} In this example, we draw a straight line from (0,0) to (2,2) using the \draw command. We have added the option $\verb|postaction={decorate}|$ to decorate the path with the previously defined markings. The latter are defined by the key $\verb|decoration={options}|$ used with the $\verb|tikzpicture|$ environment.

The $\verb|mark|$ decoration option is used to specify a marking (arrowhead in this case) in two different positions along the path:

• dimensionless non-negative number: it sets the marking at a position relative to the path length, 0.25 means put the arrowhead at 25% of the path.
• non-negative dimension number: it puts the arrowhead at a given distance (2cm) from the start point of the path in question.

If you consider negative values of the above options, then the positioning will be from the end point of the path and not the start point.

Here is another illustrative example (repeated arrows):

\documentclass[border=0.2cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{ decorations.markings}

\begin{document}

\begin{tikzpicture}
[decoration={markings,
mark= between positions 0.25 and 0.75 step 2mm
with {\arrowreversed{latex}}}
]

\draw [postaction={decorate}] (0,0) -- (2,2);

\end{tikzpicture}

\end{document} The above draws repeated arrows, with a step of 2mm, between the positions 0.25 and 0.75 of the path length. In this case, we used $\verb|\arrowreversed{}|$ instead of $\verb|\arrow{}|$ to invert the direction of the arrow tip. You may remarked that we have changed the arrow tip, we used $\verb|latex|$ tip instead of $\verb|stealth|$. I think, we should stop here and for more details, you can check the PGF package manual.

### Method 3: TikZ code inside a node

The basic idea is to create a TikZ code for the arrow and include it in a node along your path. Here is an illustrative example:

\documentclass[border=0.2cm]{standalone}

\usepackage{tikz}

% create an arrow
\newcommand{\arrowIn}{
\tikz \draw[-stealth] (-1pt,0) -- (1pt,0);
}

\begin{document}

\begin{tikzpicture}

\draw (0,0) -- (2,2) node[
sloped,
pos=0.5,
allow upside down]{\arrowIn}; ;

\end{tikzpicture}

\end{document} We created a new command, named $\verb|\arrowIn|$, that mainly draws an arrow with a tip style $\verb|-stealth|$. This command is used inside a node where the same options of method 1 can be used. It should be noted that we added the option $\verb|allow upside down|$ to force the arrow direction to follow the path flow. Without this option, if we draw the same path but from (2,2) to (0,0) then we get the same arrow direction as above. This also has to be added to method 1 (no need to add it in method 2).

Conclusion: To be honest, I used to use method 3 as it allows me to control everything in my code. Well, I am lazy to go back to the wonderful manual and read more 😂. Method 1 is a quick solution BUT Method 2 is what I recommend.

That's all for today!

I hope you found it useful 😊. If you have any remarks or suggestions, leave us a comment!

Thanks ❤️❤️❤️

0 0 vote
Article Rating
Subscribe
Notify of 