# Side-by-side subfigures, MATLAB export, textwidth

EDIT: This might be solved with just the knowledge of what size \textwidth in points or centimeters is with this setup.

I have the problem of exporting MATLAB figures to LaTeX and keeping the font size of the axis labels. I want to achieve the right export size to have two figures side-by-side reaching the whole textwidth. (See the figure which have too small axis font size)

As I don't export my figures with the right width in MATLAB, LaTeX will scale the picture and the axis labels will change in size. MATLAB's export setup allows me to export exact size of the figure in points or centimeters with the right font size of the axis labels.

What width should I use when exporting from MATLAB and how should I implement the code in LaTeX to fulfill this?

\documentclass[12pt, a4paper]{article}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[font={small,it}]{caption}
\usepackage{subcaption}
\usepackage{graphicx}

\begin{document}

\begin{figure}
\centering
\begin{subfigure}{0.45\textwidth}
\centering
\includegraphics[width = \textwidth]{B1_coarse.png}
\caption{Coarse mesh with $$h_{max} = 1/2$$.}
\label{fig:b1_coarse}
\end{subfigure}
~
\begin{subfigure}{0.45\textwidth}
\includegraphics[width = \textwidth]{B1_fine.png}
\caption{Fine mesh with $$h_{max} = 1/32$$.}
\label{fig:b1_fine}
\centering
\end{subfigure}
\end{figure}

\end{document}

• There's not much you can do on the LaTeX side because LaTeX doesn't modify the picture in any way, therefore this question is off topic in this site. My suggestions to you: You can change the font size directly in Matlab with set(gca,'FontSize',16), for instance. You can also export the figure to svg (although it might get large for this kind of picture) and export it to LaTeX with inkscape. Finally, you can make the picture directly in LaTeX with pgfplots. Dec 12 '18 at 14:34
• @PhelypeOleinik do you know what size \textwidth is for me with this setup? Then I might be able to just export as a little bit less than half of that value. Dec 12 '18 at 14:36
• The value of \textwidth varies with your document setup, but you can find out very easily. Just type \the\textwidth somewhere in the text and TeX will print the value of \textwidth in pt :) P.S.: In the occasion of your edit, then your question is a duplicate of this one Dec 12 '18 at 14:49
• unrelated but use h_{\mathrm{max}} (never use math italic for multi-letter words) also would be better to use \end{subfigure}\hfill\begin{subfigure} rather than \end{subfigure} ~ \begin{subfigure} currently you have .1\textwidth to fill (as your subfigures add up to .9) but you put three word spaces between, which might not be able to shrink enough (forcing the second figure in to the margin) or might not be able to stretch enough (leaving a gap to the right of the second figure) Dec 12 '18 at 15:21

First of all find out how how wide \textwidth is in your document by typing in \the\textwidthin your code and the width value will be written in points when compiled.

In MATLAB, create the figure with desired font sizes.

Under the figure, go to: File -> Export Setup

Important to do this as the first step: Go to "Fonts" under "Properties" and uncheck "Custom Size:".

Then go back to "Size", check "Expand axes to fill figure".

Choose "points" under "Units:".

Under "Width", write in half of the \textwidth value and choose an appropriate "Height".

(Tips is to export this setup using the tool below the dimensions-)

Press "Apply to Figure" and then "Export...", preferably save as .eps or if way too large file save as .png.

Then use the following simple code:

\begin{figure}[H]
\centering
\begin{subfigure}{0.495\textwidth}
\centering
\includegraphics[width=\linewidth]{B1_fine.png}
\caption{Coarse mesh with $$h_{max} = 1/2$$.}
\end{subfigure}
\hfill
\begin{subfigure}{0.495\textwidth}
\centering
\includegraphics[width=\linewidth]{B1_fine.png}
\caption{Fine mesh with $$h_{max} = 1/32$$.}
\end{subfigure}
\end{figure}