3

I would like to draw the below picture:
enter image description here
I have achieved to do the following:
enter image description here Here is the MWE:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\filldraw[color=lightgray] (1,0) rectangle (7,4);
\filldraw[color=blue] (1.5,3) rectangle (2.5,0.5);
\filldraw[color=blue] (5.5,3) rectangle (6.5,0.5);
\filldraw[color=white] (3.5,1.2) rectangle (4.5,2.4);
\end{tikzpicture}
\end{document} 

My question is, how to draw these red lines from the North pole to south pole through the intermediate square?

subham soni
  • 9,673

3 Answers3

6

Just for comparison, here's a way to draw the effect lines in Metapost.

prologues := 3;
outputtemplate := "%j%c.eps";

beginfig(1);

u = 1cm;

% background and the central whitish box
fill unitsquare xscaled 8u yscaled 5u shifted (-4u,-5/2u) withcolor .8 white;
fill unitsquare scaled u shifted (-1/2u,-1/2u) withcolor .95 white;
draw unitsquare scaled u shifted (-1/2u,-1/2u);

% parameters
-z1 = z2 = (2.4u,0);
a := 4/5;
b := 2/5;
c := 4/5;

% draw the effect lines
for i = -c step c/5 until c+eps:
  s := if i<0: -sqrt(-i) else: sqrt(i) fi;
  draw (x1,i*u) .. (-a*u,i*u) {right} .. {right} (-b*u,s*u) 
    -- (+b*u,s*u) {right} .. {right} (+a*u,i*u) -- (x2,i*u) 
    withcolor .67 red;
endfor

% draw the N and S boxes
fill unitsquare xscaled u yscaled 2u shifted z1 shifted (-1/2u,-u) withcolor .3 blue + .7 white;
draw unitsquare xscaled u yscaled 2u shifted z1 shifted (-1/2u,-u);
fill unitsquare xscaled u yscaled 2u shifted z2 shifted (-1/2u,-u) withcolor .3 blue + .7 white;
draw unitsquare xscaled u yscaled 2u shifted z2 shifted (-1/2u,-u);

% add the labels
defaultfont := "phvr8r";
label("N", z1);
label("S", z2);
label(btex $\mu_r<1$, $\chi_m<0$, (weak effect) etex, (0,3/2u));

endfig;
end.

enter image description here

Thruston
  • 42,268
  • Wow.. its just awesome :)... – subham soni May 21 '14 at 14:22
  • I am not able to center the figure. \begin{center} doesn't work. What to do? – subham soni May 21 '14 at 17:34
  • @subhamsoni Are you using xelatex? If so, the TL2013 version has a bug in the way it handles EPS files from Metapost with negative coordinates. Either upgrade to the latest; or use pdflatex; or delete the line at the top of the .eps file that says %%Creator: Metapost.... – Thruston May 21 '14 at 21:45
  • I am using luatex on MikTeXt distribution. I would like to center the figure. – subham soni May 22 '14 at 02:46
6

I think the code is more flexible and maintenable if you use named nodes and coordinates, as in the following example:

\usetikzlibrary{calc}

\begin{tikzpicture}
% Background
\filldraw[color=lightgray] (1,0) rectangle (7,4);

% North pole
\node[draw, minimum width=1cm, minimum height=2.5cm, inner sep=0pt, fill=blue, anchor=south west] at (1.5, 0.5) (north pole) {\sffamily\bfseries N};

% South pole
\node[draw, minimum width=1cm, minimum height=2.5cm, inner sep=0pt, fill=blue, anchor=south west] at (5.5, 0.5) (south pole) {\sffamily\bfseries S};

% White block
\node[draw, minimum width=1cm , minimum height=1.2cm, inner sep=0pt, fill=white, anchor=center] at ($(north pole)!.5!(south pole)$) (block) {};

% Vertical points at which the field lines are distorted
% as percentage of the distance between poles and white block
\coordinate (outer left edge) at ($(north pole.east)!.8!(block.west)$);
\coordinate (inner left edge) at ($(block.west)!.2!(block.east)$);
\coordinate (inner right edge) at ($(block.west)!.8!(block.east)$);
\coordinate (outer right edge) at ($(block.east)!.2!(south pole.west)$);

% To draw the field lines, I use a for loop. The numbers in the list
% is the vertical distance each line is deflected
\foreach \p [count=\i] in {0.1,0.2,0.3,0.4,0.3,0,-0.4,-0.4,-0.3,-0.2,-0.1} {
  % Vertical coordenate of the undeflected line
  \coordinate (aux1) at ($(north pole.north east)!\i*1/12!(north pole.south east)$);
  % Vertical coordinate of the deflected line
  \coordinate (aux2) at ($(aux1)+(0,\p)$);

  \draw[red, rounded corners=2pt] (aux1) -- (aux1-|outer left edge) --
       (aux2-|inner left edge) -- (aux2-|inner right edge) -- 
       (aux1-|outer right edge) -- (aux1-|south pole.west);
}
\end{tikzpicture}

Which produces:

Result

The advantage of this approach is that it is simple to change the dimensions, for example of the poles, and have the figure automatically adjusted. For example, changing the height of the poles to 2cm:

% North pole
\node[draw, minimum width=1cm, minimum height=2cm, inner sep=0pt, fill=blue, anchor=south west] at (1.5, 0.5) (north pole) {\sffamily\bfseries N};

% South pole
\node[draw, minimum width=1cm, minimum height=2cm, inner sep=0pt, fill=blue, anchor=south west] at (5.5, 0.5) (south pole) {\sffamily\bfseries S};

the new figure is:

New result

JLDiaz
  • 55,732
3

If you want to just fudge the numbers, use the curve to library.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\filldraw[color=lightgray] (1,0) rectangle (7,4);
\filldraw[color=blue] (1.5,3) rectangle (2.5,0.5);
\filldraw[color=blue] (5.5,3) rectangle (6.5,0.5);
\filldraw[color=white] (3.5,1.2) rectangle (4.5,2.4);
\draw[color=red]  (2.5,3) -- (3.2,3) to[in=180,out=0] (3.5,3.1) -- (4.5,3.1) to[in=180,out=0] (4.8,3) -- (5.5,3);% repeat
\end{tikzpicture}
\end{document} 

first line

John Kormylo
  • 79,712
  • 3
  • 50
  • 120