LaTeX More in figures: Side-by-Side Figures
Tags: latex, codenewbie
Published: 2021-03-17 15:57:00

Hello hello, how are you? :) 

These days I have been writing about images in LaTeX, this is an important thing because you can just put an image to decorate your file or, for example, if you are working with some investigation you can put images to show the results of the analysis you did. 

In [this](https://dev.to/latexteada/latex-importing-images-4fhj) post I teach you how to import a simple image, and in [this](https://dev.to/latexteada/latex-figures-the-figure-environment-5a75) I teach you how to do more things with your image, like adding a caption. I suggest you read these before you start with this post because I use some commands which I have talked about them there

Today I write about Side-by-Side images

# Side-by-Side Images

This is a very useful tool because you can contrast the differences in the images which you are using. Or just because you want to add multiple images. 

This task is so easy to do, we just need to add the images in different `includegraphics` command inside the `figure` environment 

> Do not forget that you need the `graphicx` package imported in the preamble 

Before the example, I want to tell you 

* The total width of all the figures must not exceed the page width. Otherwise, the figures will be inserted one below other
* There must not be any line breaks between the `includegraphics` command

An example is 
![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/szm4g0bdj9emgge0rpwi.png)

Produces 

![Screenshot from 2021-03-15 10-13-21](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fphdtldaqa3nfphgovon.png)

We can use `\hfill` to separate the figures by the available horizontal space 

![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q7dyxm55ss5dvlcfyt1v.png)
 
Produces 

![Screenshot from 2021-03-15 10-21-49](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n5bc7lbuu1ivu1ffzqj3.png)
 
> Maybe the difference is not so big, but, you can try with different sizes of images and you will notice the difference 

That is how you can put side-by-side images in LaTeX, but did you notice?

> There is just one caption for all the images you have put

Sometimes it could be good, but sometimes it is not good because you want to explain each image, and of course, there is a way to do it :) 

# The `minipage` environment

`minipage` as its name suggests is like if you have a mini page inside the page, you can configure its width and the content it will have 

```
\begin{minipage}[position]{width}
content
\end{minipage}
```

Where `position` could be 
* `c` center
* `t` at the top
* `b` at the bottom 

And `width` can be
* An explicit measure, for example, *3cm*, *8cm*
* A portion of the width of the page, we can do it with `X\linewidth` where *X* is the percentage of the page, *0<X<1*, for example, `0.5\linewidth* is the 50% of the page's width 

An example 

![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ea1g4rs1x3ez3hwi8qhs.png)

Produces 

![Screenshot from 2021-03-16 10-41-59](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ymav88ruaxlzft4rr84x.png)

# Images and `minipage`

If you want to add multiple images with a different caption, you need to put those in the `minipage` environment 

> Remember that you need to have the `graphicx` package imported

* You can add a general caption, just before the `\end{figure}` command 

An example 
![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ic01tg9ytshyy550vzow.png)

Produces
 ![Screenshot from 2021-03-16 10-55-15](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s56brnwt1rbrcr95mma9.png)
  
Thanks, that was all, do not forget to follow me on Twitter `@latexteada`