📜 ⬆️ ⬇️

Diagrams in LaTeX

Many quite often face the need to create various diagrams, graphs, trees for convenient presentation of information. This issue may be especially important when creating presentations. Most office suites offer the ability to create beautiful diagrams using an interactive interface. And if you need to create a large chart? Or write in her mathematical formulas? Focus on the content, rather than the design and arrangement of elements on the screen?

The benefits of using LaTeX have been repeatedly discussed . As well as ways to create presentations using beamer and vector graphics from the package PGF / Tikz. But is it possible to get diagrams in LaTeX that are not inferior in appearance to those obtained in large and complex packages? One way is suggested below.

Start


First we need LaTeX ( MiKTeX is suitable for Windows, TeXlive for Linux or Mac), as well as the beamer and tikz packages . Both are included with the MiKTeX. You can download the latest versions either from the project pages or from CTAN . You will also need basic knowledge of LaTeX, and the use of these packages in it. Since beamer is focused on using pdflatex, PDF will be used primarily.

Simple chart


Let's try to make a small and simple diagram. As an example, let's take a serial format conversion when LaTeX works: .tex> .dvi> .ps> .pdf . Suppose we already have a document in which to place this diagram. In the preamble, we include the tikz package and connect the necessary libraries. In this case, we will need positioning and arrows, which are needed to position the elements relative to each other and draw arrows between them, respectively.
\usepackage { tikz }
\usetikzlibrary { positioning,arrows } <br>
It should be noted that spaces are not allowed when listing library names separated by commas. This is due to the fact that tikz completely substitutes the string as part of the name of the file with the library.
In the right place of the document, we add the tikzpicture environment, inside which the tikz commands are listed. Each team must end with a semicolon. Commands can be nested, for example to create an arrow with a label or child elements. The general command syntax is:
\command [parameters] (name) {contents} arguments;
WhereAll parts except the command itself are optional. In tikz hundreds of commands, path and node will be interesting for us.
The \ path command creates a “path” in the terminology of vector graphics. As a parameter, it is indicated what exactly should be done with this contour:Also indicate the presence and direction of arrows <-, ->, <-> , color, line thickness and other parameters; a full list can be found in the package documentation.
As arguments, coordinates are passed through which the path must pass. In tikz there are many options for specifying coordinates:Points between which it is required to draw a line are divided by the operator - . The positioning with respect to objects is most suitable for the ideology of LaTeX. For example, to draw a connection between the edges of foo and bar objects, just run the command
\path (foo) edge (bar);<br>

The \ node command creates a node (or object), usually containing some text. Its parameters can be text style, color, information on the presence, shape and color of outlines, location relative to other objects and many others. You can locate a node in a specific location with coordinates (x, y) using the argument at (x, y) . But the most interesting is the location relative to other objects provided by the positioning library. To use it, it is enough to specify in the parameters in which direction relative to another object this one should be. for example
\node (foo) { foo } ;<br>
\node [ right of=foo ] (bar) { bar } ;<br>

More information is available in the documentation. The above is quite enough to create the first simple diagram. It should have 4 objects, and 3 arrows between them. The implementation looks like this
\node (tex) { .tex } ;<br>
\node [ right of=tex ] (dvi) { .dvi } ;<br>
\node [ right of=dvi ] (ps) { .ps } ;<br>
\node [ right of=ps ] (pdf) { .pdf } ;<br>
<br>
\path [ -> ] (tex) edge (dvi);<br>
\path [ -> ] (dvi) edge (ps);<br>
\path [ -> ] (ps) edge (pdf);<br>


')

Improving the chart


The first picture we already had, but it does not look at all: an offset line of text, no frames around the text, too close objects. And this instead of beautiful pictures with gradients and transparency? The diagram is definitely worth improving, and for this we will use the task of styles. The style is set using the command
\tikzstyle { name } = [ parameters ]
The style name can then be passed as a parameter to another command. For further work, we also need to connect the shapes and shadows libraries. A rectangle with rounded edges, which is in the library, is a good figure for text. For him additionally need to describe the background and contour. Before we do this, let's understand how to set colors in tikz.
The color can be specified by name from the predefined in LaTeX and xcolor package. You can also define your RGB color with the command
\definecolor { name }{ rgb }{ 0.5,0.5,0.5 }
Comma-separated RGB-components and their number from 0 to 1. Or in other systems, about which you can read more in the xcolor documentation. Also predefined colors can be mixed. The syntax for this operation is:
color1 ! percent ! color2
The result is a color containing percent % of the first color and (100-percent) % of the second. If the second is not specified, the default is white. Mixing colors can be repeated without retaining the intermediate color, for example
red ! 50 ! black ! 50 ! white
First, the color will be obtained from 50% red and 50% black, and then the result is mixed with 50% white. In the resulting red and black will be 25%.

Now we can go directly to the task of style
\tikzstyle { format } = [ rounded rectangle,<br>
thick,<br>
minimum size= 1cm ,<br>
draw=red!50!black!50,<br>
top color=white,<br>
bottom color=red!50!black!20,<br>
font= \itshape ,<br>
drop shadow ] <br>
This style requires to draw a rectangle with rounded edges, a bold contour line with the color red! 50! Black! 50 , fill it using a gradient from top to bottom from white to red! 50! Black! 20 , with a shadow, print the inner text in italics.
Next we need to align the baseline of the text. To do this, first understand why he was displaced. The words "tex", "ps" and "pdf" have different heights, and accordingly were centered in various ways inside the object. Thus, explicitly setting the height of the text should solve this problem.
The tikzpicture environment also allows you to set parameters that apply to all commands within this environment. We will use this and set the thickness of the communication line, the minimum distance between objects and the text height parameters in it.
\begin { tikzpicture }[ thick,<br>
node distance=2cm,<br>
text height=1.5ex,<br>
text depth=.25ex ] <br>
\node [ format ] (tex) { .tex } ;<br>
\node [ format,right of=tex ] (dvi) { .dvi } ;<br>
\node [ format,right of=dvi ] (ps) { .ps } ;<br>
\node [ format,right of=ps ] (pdf) { .pdf } ;<br>
<br>
\path [ -> ] (tex) edge (dvi);<br>
\path [ -> ] (dvi) edge (ps);<br>
\path [ -> ] (ps) edge (pdf);<br>
\end { tikzpicture } <br>



Complicate the task


Now that we’ve got an acceptable version of a simple diagram, let's try to complicate the task. Add to this chart the ways of presenting data (on screen and print) and the possibility of conversion without intermediate formats. Since there will be a lot of lines on the diagram, it is worth signing them.
Define a different node style for the presentation method. Let it be an ellipse with a blue gradient from top to bottom, a dark blue outline and a shadow. The style description will be similar to the previous one.
\tikzstyle { format } = [ ellipse,<br>
thick,<br>
minimum size= 1cm ,<br>
draw=blue!50!black!50,<br>
top color=white,<br>
bottom color=blue!50!black!20,<br>
drop shadow ] <br>

But adding additional lines poses another problem. If you set the path from (tex) to (pdf) tikz by default, it will draw a straight line that does not suit us at all. Unfortunately, the package does not know how to use graphviz yet, so you have to describe the way around the elements yourself. For this we will use the relative task paths.
\path [ ->, draw ] (tex) -- +(0,2) -| (pdf);<br>
\path [ ->, draw ] (dvi) -- ++(0,1) -- ++(3,0) -- (pdf);<br>
The first circuit starts from the node (tex) and rises 2 relative units up. After that, it continues to the right until a perpendicular to the node is formed (pdf) and goes down to it. The second contour starts from the node (dvi) , rises by 1 unit, goes to the right by 3 units relative to the previous point, and goes straight to the node (pdf) . To add labels to the contours, use the nested commands and add the node to the contour:
\path [ ->, draw ] (tex) -- +(0,2) -| node [ near start ] { pdf \LaTeX } (pdf);<br>

But this command will add an object so that the centers of the contour and the node coincide. The text will be crossed out line of communication. To prevent this from happening in the tikzpicture environment, select the auto positioning option for auto signatures. Now the description of our chart will look like
\begin { tikzpicture }[ thick,<br>
node distance=3cm,<br>
text height=1.5ex,<br>
text depth=.25ex,<br>
auto ] <br>
\node [ format ] (tex) { .tex } ;<br>
\node [ format,right of=tex ] (dvi) { .dvi } ;<br>
\node [ format,right of=dvi ] (ps) { .ps } ;<br>
\node [ format,right of=ps ] (pdf) { .pdf } ;<br>
\node [ medium,below of=dvi ] (screen) { screen } ;<br>
\node [ medium,below of=ps ] (verbatim) { verbatim } ;<br>
<br>
\path [ -> ] (tex) edge node { \LaTeX } (dvi);<br>
\path [ -> ] (dvi) edge node { dvips } (ps);<br>
\path [ -> ] (ps) edge node { ps2pdf } (pdf);<br>
\path [ -> ] (dvi) edge node { xdvi } (screen);<br>
\path [ -> ] (ps) edge node [ near start ] { gs } (screen);<br>
\path [ -> ] (pdf) edge (screen);<br>
\path [ -> ] (ps) edge node { print } (verbatim);<br>
\path [ -> ] (pdf) edge (verbatim);<br>
\path [ ->, draw ] (tex) -- +(0,2) -| node [ near start ] { pdf \LaTeX } (pdf);<br>
\path [ ->, draw ] (dvi) -- ++(0,1) -- node [ near start ] { dvipdf } ++(3,0) -- (pdf);<br>
\end { tikzpicture } <br>



We use in the presentation


The resulting diagram can be used as a static image in the article or description. But for presentation, it is often required that the diagram objects appear in a specific order. In LaTeX, the beamer package is used to create presentations. His work is built around the concept of overlays - different representations of the same slide. We have already written about the use of beamer, so let us recall the overlay specification. They can be added to almost any LaTeX team, and tikz commands are no exception. Therefore, any illustrations obtained using this package can be easily divided into parts and integrated with beamer. However, now the teams are not quite convenient for adding overlays. If you want the object to appear at the same time and all of its links to existing ones, you will have to add specifications to each of the commands. It would be more logical to arrange the teams in sequence according to the time of appearance on the slide. Here comes the opportunity to group the teams, which was mentioned at the very beginning. In the \ path command, you can specify the nodes that it will connect. Thus, we can make one team for each overlay.
\begin { frame } <br>
\frametitle { \LaTeX workflow } <br>
\begin { tikzpicture }[ thick,<br>
node distance=3cm,<br>
text height=1.5ex,<br>
text depth=.25ex,<br>
auto ] <br>
<br>
\path [ use as bounding box ] (-1,0) rectangle (10,-2);<br>
\path [ -> ] <1-> node [ filename ] (tex) { .tex } ;<br>
\path [ -> ] <2-> node [ filename, right of=tex ] (dvi) { .dvi } <br>
(tex) edge node { \LaTeX } (dvi);<br>
\path [ -> ] <3-> node [ display, below of=dvi ] (screen) { screen } <br>
(dvi) edge node { xdvi } (screen);<br>
\path [ -> ] <4-> node [ filename, right of=dvi ] (ps) { .ps } <br>
(dvi) edge node { dvips } (ps);<br>
\path [ -> ] <5-> (ps) edge node { gs } (screen);<br>
\path [ -> ] <6-> node [ display, below of=ps ] (verbatim) { verbatim } <br>
(ps) edge node { print } (verbatim);<br>
\path [ -> ] <7-> node [ filename, right of=ps ] (pdf) { .pdf } <br>
(ps) edge node { ps2pdf } (pdf);<br>
\path [ -> ] <8-> (pdf) edge (screen)<br>
edge (verbatim);<br>
\path [ ->, draw ] <9-> (tex) -- +(0,2) -| node [ near start ] { pdf \LaTeX } (pdf);<br>
\path [ ->, draw ] <10-> (dvi) -- ++(0,1) -- node [ near start ] { dvipdf } ++(3,0) -- (pdf);<br>
\end { tikzpicture } <br>
\end { frame } <br>



What happened as a result can be viewed here .
_________
In the preparation used
PGF / Tikz manual
Xcolor manual
Beamer manual
The TeX workflow example

Source: https://habr.com/ru/post/81751/


All Articles