4

enter image description here

I want to draw the following diagram in LaTeX:

enter image description here

CarLaTeX
  • 62,716
  • You could youre the TikZ package, you will find a short tutorial at the begin of the documentation. – poch Sep 25 '18 at 11:04

4 Answers4

12

For the sake of completeness, here it is a plain TikZ approach

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
    \begin{tikzpicture}[node distance = 2cm, thick]% 
        \node (1) {1};
        \node (2) [right=of 1] {2};
        \node (3) [right=of 2] {3};
        \draw[->] (1) -- node [midway,above] {a} (2);
        \draw[->] (1) to [bend right] node [midway,below]{c} (3);
        \draw[->] (2) -- node [midway,above] {b} (3);
    \end{tikzpicture}%
\end{document}

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
    \begin{tikzpicture}[node distance = 2cm, thick]% 
        \node (1) {1};
        \node (2) [right=of 1] {2};
        \node (3) [right=of 2] {3};
        \node (dots) [right=of 3] {$\cdots$};
        \node (n1) [right=of dots] {$n-1$};
        \node (n) [right=of n1] {$n$};
        %
        \draw[->] (1) -- node [midway,above] {$a_1$} (2);
        \draw[->] (1) to [bend right=15cm] node [midway,below]{b} (n);
        \draw[->] (2) -- node [midway,above] {$a_2$} (3);
        \draw[->] (3) -- node [midway,above] {$a_3$} (dots);
        \draw[->] (dots) -- node [midway,above] {$a_{n-2}$} (n1);
        \draw[->] (n1) -- node [midway,above] {$a_{n-1}$} (n);
    \end{tikzpicture}%
\end{document}

enter image description here

Diaa
  • 9,599
10

It's quite easy with tikz-cd:

enter image description here

\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}
1 \ar[r,"a"] \ar[rr,bend right,"c"'] & 2 \ar[r,"b"] & 3
\end{tikzcd}
\end{document}

But, in my humble opinion, I'd vote for picture-mode, for its far more user-friendly syntax:

enter image description here

\documentclass{article}
\usepackage{pict2e}
\begin{document}
\setlength{\unitlength}{1cm}
\begin{picture}(2.1,0.75)(0,-0.45)
\put(0,0){1}
  \put(0.2,0.1){\vector(1,0){0.75}}
  \put(0.5,0.15){\footnotesize$a$}
\put(1,0){2}
  \put(1.2,0.1){\vector(1,0){0.75}}
  \put(1.5,0.15){\footnotesize$b$}
\put(2,0){3}
  \qbezier(0.2,-0.05)(1.05,-0.7)(1.9,-0.05)
  \put(1.9,-0.05){\vector(4,3){0.0001}}
  \put(1.0,-0.55){\footnotesize$c$}
\end{picture}
\end{document}

Update: tikz-cd solution for the second diagram:

enter image description here

\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}
  1 \ar[r,"a_1"]
    \ar[rrrrr,bend right=50,looseness=0.3,"b"']
& 2 \ar[r,"a_2"]
& 3 \ar[r,"a_3"]
& \cdots
  \ar[r,"a_{n-2}"] % This arrow isn't in the picture, but I chose to add it. Feel free to remove.
& n-1 \ar[r,"a_{n-1}"]
& n
\end{tikzcd}
\end{document}
5

A pstrickssolution: the three numbers are defined as nodes, and we link them by node connections.

\documentclass[12pt]{article}
\usepackage{pst-node, auto-pst-pdf}

\begin{document}

\[
  \Rnode{1}{1} \hspace{3em} \Rnode{2}{2} \hspace{3em} \Rnode{3}{3}
\psset{linewidth=0.6pt, arrows=->, arrowinset=0.12, nodesep=0.6ex, labelsep=2pt, shortput=nab}
\everypsbox{\scriptstyle}
\ncline{1}{2}^{a}\ncline{2}{3}^{b}
\ncarc[arcangle=-30]{1}{3}_{c}
\]

\end{document} 

enter image description here

Bernard
  • 271,350
3

With TikZ, it is possible to use the chain concept of the graphs library.

chains are a series of nodes whose syntax is abbreviated compared to their normal syntax. A chain ends with a semicolon. The nodes and their text are identical by default as in the first figure. Nodes and text are the number 1, 2 and 3.

This first figure is composed of 2 chains: the first 1->2->3 and the second which goes directly from 1 to 3.

To add text to the arrow (that is called edge by tikz), simply place the option edge label or edge label' depending on whether the text is above or below the arrow. In reality, this places the text to the left or right when walking in the direction of the chain.

In the second example, to avoid having to draw an arrow, we write -!- instead of -> (see section 19.3.3 Syntax of Chain Specifications page 272 of 3.1.1 TikZ manual). To make the node text different from its name, simply separate the name from the text with a slash. For example : 5/{$n-1$} means that node 5 displays the n-1 text.

With the quotes library:

Using the quotes library, the syntax for writing labels is even shorter: "a" instead of edge label={a} "c"' instead of edge label' ={c} You can customize the colors as below:

chaine

\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\usetikzlibrary{graphs,quotes}
\begin{document}

\tikz[thick,gray] \graph[grow right=30mm] {
1 ->["a"] 2   ->["b"] 3;
1 -> [bend right,"c"'] 3;
};

General case:
\smallskip

\tikz[thick,every edge/.style={red,draw},every node/.style={blue}] \graph[grow right=20mm,edge quotes={black,auto}] {
1 ->["$a_1$"] 2   ->["$a_2$"] 3->["$a_3$"] 4/$\cdots$   -!- 5/{$n-1$}->["$a_{n-1}$"] n  ;
1 -> [bend right,"b"'] n;
};

\end{document}

Without the quotes library

chaine-tikz

\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\usetikzlibrary{graphs}
\begin{document}

\tikz \graph[grow right=30mm] {
1 ->[edge label={a}] 2   ->[edge label={b}] 3;
1 -> [bend right,edge label'={c}] 3;
};


\tikz \graph[grow right=20mm] {
1 ->[edge label={$a_1$}] 2   ->[edge label={$a_2$}] 3->[edge label={$a_3$}] 4/$\cdots$   -!- 5/{$n-1$}->[edge label={$a_{n-1}$}] n  ;
1 -> [bend right,edge label'={b}] n;
};

\end{document}

Translated with www.DeepL.com/Translator

AndréC
  • 24,137