2

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:

two subfigures, one overlays the other

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?

Blub Bla
  • 317
  • dont know about gnuplottex, but have you tried capsuling the plot into a minipage (so to pass the correct widths) – Bort Aug 07 '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 07 '15 at 13:13
  • See http://tex.stackexchange.com/questions/252152/how-can-the-size-of-an-epslatex-terminal-plot-with-gnuplottex-be-altered/252463#252463 for a number of possibilities to change the size of a gnuplot graph. – John Aug 07 '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 07 '15 at 13:50

4 Answers4

2

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}
Blub Bla
  • 317
1

None of the above worked for me, so here's my solution, adapted from here:

% first define the conversion command in the preamble
\makeatletter
\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1}
\makeatother

% then use it like this \begin{gnuplot}[terminal=epslatex, terminaloptions={size \convertto{cm}{\textwidth}cm, 7cm font 8}] ...

Took me only 3 hours, so enjoy :)

0

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
{\catcode`p=12 \catcode`t=12 \gdef\cm#1pt{#1cm}} 
    \begin{gnuplot}[terminal=tikz,terminaloptions={size \expandafter\cm\the\widthgnuplot,\expandafter\cm\the\heightgnuplot}]
        plot sin(x)
    \end{gnuplot}
John
  • 1,495
0

I just added gnuplot in my very new library robust-externalize. You can do this as follows:

\documentclass{article}

\usepackage{lipsum} % for dummy text

\usepackage{tikz} % Needs version 2.3 (as of 2023/12/18, not yet pushed to CTAN, so download .sty here https://github.com/leo-colisson/robust-externalize) \usepackage{robust-externalize} \robExtConfigure{enable fallback to manual mode} % If you forgot -shell-escape, it gives instructions on how to manually compile the file

\begin{document}

\lipsum[1]

\noindent\begin{CacheMeCode}{gnuplot, cairolatex terminal/.expanded={size \lenToCm{\textwidth},\lenToCm{.5\textwidth}}} plot sin(x) \end{CacheMeCode}

\end{document}

tobiasBora
  • 8,684