2

How can I draw the following diagram in LaTeX? But instead of the black dots in vertices, I want to put the numbers. enter image description here

Liu Li
  • 31

2 Answers2

4

For this one we need the following TikZ libraries:

  1. calc to place some nodes at the middle point between another two:
\node (N) at ($(A)!0.5!(B)$) {}; % places the node (N) midway between (A) and (B)
  1. backgrounds to draw the ellipses last, but on background layer (behind the nodes). This option needs a scope.

For the rest, first place all the nodes, then draw the lines (as said, on background layer). As the picture has two graphs almost identical, we can use a \foreach loop to place the left and right nodes with the same code, and another one for the the numbered nodes.

Like this:

\documentclass[tikz,border=1.618mm]{standalone}
\usetikzlibrary{calc,backgrounds}

\definecolor{aquamarine}{HTML}{008B9B} \tikzset{my node/.style={draw,circle,fill=white,inner sep=0,minimum size=9mm}}

\begin{document} \begin{tikzpicture} \def\a{4.5} % a semiaxis \def\b{2.25} % b semiaxis \def\h{2.5} % height \foreach[count=\jj from 0]\j in {L,R} % left, right { \begin{scope}[shift={({2\jj(\a+0.5)},0)}] \foreach[count=\ii]\i in {130,210,250,290,330} {% nodes 1 to 5 \node[my node] (\j B\ii) at (\i:\a cm and \b cm) {$\ii'$}; \node[my node,yshift=\h cm] (\j T\ii) at (\i:\a cm and \b cm) {$\ii$}; }% nodes 6 and 7 \node[my node] (\j B6) at (50:\a cm and \b cm) {}; \node[my node,yshift=\h cm] (\j T6) at (50:\a cm and \b cm) {}; \node[my node] (\j B7) at (90:\a cm and \b cm) {$(2n)'$}; \node[my node,yshift=\h cm] (\j T7) at (90:\a cm and \b cm) {$(2n)$}; \foreach\i in {1,...,7}% vertical lines \draw (\j B\i) -- (\j T\i); \foreach\i in {2,4}% middle height nodes \node[my node] at ($(\j B\i)!0.5!(\j T\i)$) {\pgfmathparse{int(\i/2)}$\bar\pgfmathresult$}; \node[rotate=-10] at ($(\j B6) !0.5!(\j T7)$) {$\cdots$}; % dots \end{scope} } % ellipses \begin{scope}[on background layer] \draw[very thick,red] (0,0) ellipse (\a cm and \b cm); \draw[very thick,aquamarine] (0,\h) ellipse (\a cm and \b cm); \draw[very thick,red] (RT7) -- (RB1.center) arc (130:450:\a cm and \b cm); \draw[very thick,aquamarine] (RB7) -- (RT1.center) arc (130:450:\a cm and \b cm); \end{scope} \end{tikzpicture} \end{document}

enter image description here

Juan Castaño
  • 28,426
2

As the TO is new at least to Tikz, let me highlight a few things in Juan's excellent answer, to pave the way a little for a novice. I'll use code extracts, which shows only required things.

Novices shouldn't be intimidated by Tikz' rich syntax, but conquer element by element, similar to the simplifications I use here. Try and buy-in.

1. Ellipses

  • standalone is a documentclass useful for drawings; replace by article and compile to see the difference
  • to define constants he uses TEX notation e.g. \def\h{4}, called by \h later
  • I put a help grid to simplify orientation a little
  • I increased \h a little to better see the centers
  • the % ellipses part is tikz-ish: \draw something , with [options] , (some, where), an ellipse by intention with (parameters) ;
  • finally to better see the ellipses's definition I put two \node s with some {text} , and [draw] its shape, i.e. frame

ellipses

Excerpt from Juans code and extensions:

\documentclass[tikz,border=1.618mm]{standalone}
\usetikzlibrary{backgrounds}

\definecolor{aquamarine}{HTML}{008B9B}

\begin{document} \begin{tikzpicture} \def\a{4.5} % a semiaxis % TEX-syntax; defines a constant \def\b{2.25} % b semiaxis \def\h{3} % height % increased separation

% ~~~ for some orientation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\draw [help lines] (-5,-3) grid (5,3);

% ellipses
\begin{scope}[on background layer]
  \draw[very thick,red]        (0,0)  ellipse (\a cm and \b cm);
  \draw[very thick,aquamarine] (0,\h) ellipse (\a cm and \b cm);

  % ~~~ indicating the centers ~~~~~~~~~~~~~~~~~~~~
  \node [draw] at (0,0)  {(0/0 - red center)};
  \node [draw] at (0,\h) {(0/\h) - green center};
\end{scope}

\end{tikzpicture} \end{document}

2. "Automatic" naming of nodes

Brilliant, and quite a bit to disgest for a novice to Tikz.

  • to illustrate I again put a simple help grid, and some text above the following
  • \foreach \j in {L,R} will iterate over the set given, here {L,R}, and variable \j will have values L and R while iterating
  • the [count=\jj from 0] option introduces a second variable \jj, giving the index or position in said list, i.e. will take the values 0 and 1
  • so \node at (\jj,0) {\j}; will be replaced by \node at ( 0 ,0) {L}; during the first loop, and so on
  • he reuses this mechanism later to generate names for node positions
  • e.g. (RT7) is used to address said node, which was generated beforehand by e.g. (\j T7), when \j had the value R during an iteration

node names

Excerpt from Juans code and extensions:

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

\begin{document} \begin{tikzpicture} % ~~~ help grid, from (0/0) to (1/2), 1 cm each ~~~~~~~ \draw [help lines] (0,0) grid (1,2); % ~~~ placing a node, with text, draw its shape ~~~~~~~~ \node [draw] at (0,2) {centered text at (0/2)};

% Juan's special, simplified
\foreach[count=\jj from 0]\j in {L,R} % left, right
{
    \node at (\jj,0) {\j};  
}

% ~~~ just to emphasize placement ~~~~~~~~~~~~~~~~~~~
\draw [dashed,thick] (0,.5) -- (0,1.5);

\end{tikzpicture} \end{document}

3. Other Tikz-syntax

It will be best to download the tikz-manual from ctan, which is also available as an online HTML-version. Let's pick up, what's left to explain or indicate to fully understand Juan's answer.

  • tikz-libraries expand Tikz; calc and backgrounds are described in the manual
  • \tikzset defines options or attributes for drawings; makes code more readable; example: \node[my node] ... uses the style defined as my node
  • \begin{} ... \end{} are environments, defined both in LaTeX and Tikz
  • scope is sometimes necessary; see the related chapter
  • \node is a Tikz-element you should know by heart, soon; see the related chapter as well as the tutorials in part I
  • yshift shifts node content, here along the y-axis; often useful for fine-tuned placements
  • $formula$ defines the math environment in LaTeX; as it's text, it can be the content of a node, like \node at (0,0) {$formula$};
  • \pgfmathparse and \pgfresult are explained late in the manual: most of the time you can do without, but for the double-circle task it's useful
MS-SPO
  • 11,519