# Rescaling gnuplottex to fit in subfigure

I would like to use gnuplot environments (from the gnuplottex package) in subfigures (from the subcaption package).

How can I change the size of the gnuplot graph, so that it will fit into the subfigure and doesn't overlay with the graph next to it?

This is the code I used:

\documentclass{scrbook}
\usepackage[utf8]{inputenx}
\usepackage[T1]{fontenc}

\usepackage{graphicx}
\usepackage{epstopdf}
\usepackage{subcaption}
\usepackage[miktex]{gnuplottex}

\begin{document}
\begin{figure}[htbp]
\begin{subfigure}[b]{.5\textwidth}
\begin{gnuplot}[terminal=cairolatex]
plot sin(x)
\end{gnuplot}
\caption{A subfigure}
\end{subfigure}%
\begin{subfigure}[b]{.5\textwidth}
\begin{gnuplot}[terminal=cairolatex]
plot sin(x)
\end{gnuplot}
\caption{Another subfigure}
\end{subfigure}
\caption{A figure}
\end{figure}
\end{document}


And this is its result:

EDIT: John commented this solution: Replace \begin{gnuplot}[terminal=cairolatex] by \begin{gnuplot}[terminal=cairolatex, terminaloptions={size 7cm, 4cm}]. Now I have follow-up questions: Is it possible to specify the size as 0.4\textwidth instead of 7cm? Could I just specify the width but not the height and keep the aspect ratio?

• dont know about gnuplottex, but have you tried capsuling the plot into a minipage (so to pass the correct widths) – Bort Aug 7 '15 at 12:57
• I have tried to replace \begin{subfigure} ... \caption{...} \end{subfigure} by \begin{minipage} ... \subcaption{...} \end{minipage}. The result is exactly the same. – Blub Bla Aug 7 '15 at 13:13
• See tex.stackexchange.com/questions/252152/… for a number of possibilities to change the size of a gnuplot graph. – John Aug 7 '15 at 13:23
• Thanks John. I used \begin{gnuplot}[terminal=cairolatex, terminaloptions={size 7cm,4cm}] and it worked. Now I have additional questions: Is there a way to write 0.4\textwidth instead of 7cm? And is it possible to keep the aspect ratio? – Blub Bla Aug 7 '15 at 13:50

I found a post on another question that answered my question: How do I change the aspect ratio of gnuplot output?

This is how I solved my problem:

\documentclass{scrbook}
\usepackage[utf8]{inputenx}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{epstopdf}
\usepackage{subcaption}
\usepackage{gnuplottex}
\usepackage{xparse}
\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\convertlen}{ O{cm} m }
{
\dim_to_unit:nn { #2 } { 1 #1 } cm
}
\ExplSyntaxOff

\begin{document}

\begin{figure}[htbp]
\centering
\begin{subfigure}[b]{.45\textwidth}
\begin{gnuplot}[terminal = cairolatex, terminaloptions = {size \convertlen{\textwidth},\convertlen{.3\textheight}}]
plot sin(x)
\end{gnuplot}
\caption{A subfigure}
\label{fig:1a}
\end{subfigure}%
\hfill
\begin{subfigure}[b]{.45\textwidth}
\begin{gnuplot}[terminal = cairolatex, terminaloptions = {size \convertlen{\textwidth},\convertlen{.3\textheight}}]
plot sin(x)
\end{gnuplot}
\caption{Another subfigure}
\label{fig:1b}
\end{subfigure}
\caption{A figure}\label{fig:1}
\end{figure}

\end{document}


Strangely enough, this does not seem to work with the cairolatex terminal, but works flawlessly for me with the tikz terminal (which may require a switch to luatex if you are not already using it. It's quite easy to switch from pdftex to luatex, though.) the main idea is shamelessly stolen from Setfan_K ( http://www.latex-community.org/forum/viewtopic.php?f=5&t=2712 )

\newlength\textwidthcm
\newlength\widthgnuplot
\newlength\heightgnuplot
\textwidthcm=.03514598035146\textwidth     %-- conversion from pt to cm
\widthgnuplot=0.4\textwidthcm              %-- conversion to desired width
\heightgnuplot=0.57142857142\widthgnuplot  %-- keep aspectratio (prefactor) for height
{\catcodep=12 \catcodet=12 \gdef\cm#1pt{#1cm}}
\begin{gnuplot}[terminal=tikz,terminaloptions={size \expandafter\cm\the\widthgnuplot,\expandafter\cm\the\heightgnuplot}]
plot sin(x)
\end{gnuplot}