How can I draw the following diagram in LaTeX? But instead of the black dots in vertices, I want to put the numbers. 
- 31
-
1Welcome to TSE. Please post a Minimal Working Example, instead of a code snippet. – José Carlos Santos May 13 '23 at 08:51
-
2What did you try so far? Without some example of your code helping out is difficult. – alchemist May 13 '23 at 08:53
-
Alternatively you can also try things like tools - What GUI applications are there to assist in generating graphics for TeX? - TeX - LaTeX Stack Exchange – user202729 May 13 '23 at 09:01
-
I have the no code that way I ask you – Liu Li May 13 '23 at 09:20
-
1Welcome. // Understandable, but unfortunately the wrong approach for this site. // Suggestion: look up the minimal introduction at ctan, e.g. via your search engine, to get some basic understanding of how Tikz works. Use this sites search box and "Related" links to find something similar, to start adjusting it to your needs. // We appreciate, when you add your attempts as code to your question (via EDIT), to show your achievements and obstacles. That's, when you'll get most out of our answers. // I'm confident, you'll make it :) – MS-SPO May 13 '23 at 10:20
2 Answers
For this one we need the following TikZ libraries:
calcto 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)
backgroundsto draw the ellipses last, buton background layer(behind the nodes). This option needs ascope.
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}
- 28,426
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
standaloneis a documentclass useful for drawings; replace byarticleand compile to see the difference- to define constants he uses TEX notation e.g.
\def\h{4}, called by\hlater - I put a
help gridto simplify orientation a little - I increased
\ha little to better see the centers - the
% ellipsespart is tikz-ish:\drawsomething , with[options],(some, where), anellipseby intention with(parameters); - finally to better see the ellipses's definition I put two
\nodes with some{text}, and[draw]its shape, i.e. frame
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
helpgrid, and some text above the following \foreach \j in {L,R}will iterate over the set given, here{L,R}, and variable\jwill 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\jhad the valueRduring an iteration
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;
calcandbackgroundsare described in the manual \tikzsetdefines options or attributes for drawings; makes code more readable; example: \node[my node]... uses the style defined asmy node\begin{}...\end{}are environments, defined both in LaTeX and Tikzscopeis sometimes necessary; see the related chapter\nodeis a Tikz-element you should know by heart, soon; see the related chapter as well as the tutorials in part Iyshiftshifts 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$};\pgfmathparseand\pgfresultare explained late in the manual: most of the time you can do without, but for the double-circle task it's useful
- 11,519


