11

How do I change the aspect ratio of the canvas of a gnuplot plot in LaTeX using the gnuplottex package?

The following changes the aspect ratio of the output but not the canvas size, thus leaving a margin above and below the graph.

\documentclass{scrreprt}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[miktex,cleanup]{gnuplottex}   %miktex option for my editor
\usepackage{graphicx}
\usepackage{epstopdf}

\begin{document}

\begin{figure}[htbp]
    \centering
    \begin{gnuplot}[terminal=cairolatex]
    set size ratio 0.2
    plot [0:2*pi] sin(x) with lines linewidth 2 title 'Sine'
    \end{gnuplot}
\end{figure}

\end{document}

Output of the above

yo'
  • 51,322
Don
  • 343
  • 2
  • 3
  • 8
  • 2
    You can use the terminaloptions parameter to set the canvas size: \begin{gnuplot}[terminal=cairolatex, terminaloptions={size 14cm, 4cm}]. But I don't know how to pass the \textwidth into this parameter. – Christoph Sep 17 '13 at 09:24
  • Sorry about the textwidth, it was about a question I wanted to ask earlier. I changed the title accordingly. And thanks for the workaround. – Don Sep 17 '13 at 09:33
  • My earlier comment is likewise valid for the new question. The terminal option size is used to change the canvas. Unfortunately, there is no option crop for the gnuplottex package. So you may need to play around with the gnuplot margins e.g. set tmargin or set size (without ratio) and set origin. – Christoph Sep 17 '13 at 10:02
  • @Don Congrats on the 10k. – Saphar Koshet Sep 30 '18 at 03:42

3 Answers3

8

I'm not fluent in gnuplot, so this could be refined. You can pass parameters based on \textwidth, as long as they are in the options to gnuplot:

\documentclass{scrreprt}

\usepackage{lipsum}
\usepackage[cleanup]{gnuplottex}
\usepackage{xparse}
\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\convertlen}{ O{cm} m }
 {
  \dim_to_decimal_in_unit:nn { #2 } { 1 #1 } cm
 }
\ExplSyntaxOff

\begin{document}

\lipsum[2]

\begin{figure}[htbp]
    \centering
    \begin{gnuplot}[
      terminal=cairolatex,
      terminaloptions={size \convertlen{\textwidth},\convertlen{.3\textwidth}}
    ]
    set size ratio 0.3
    plot [0:2*pi] sin(x) with lines linewidth 2 title 'Sine'
    \end{gnuplot}
\end{figure}

\lipsum[2]

\end{document}

The \convertlen macro is just to provide gnuplot with something it knows (centimeters, in this case). The command has also an optional argument for specifying another unit.

enter image description here

egreg
  • 1,121,712
  • I get an error with your code: ! Undefined control sequence. \convertlen code #1#2->\dim_to_unit:nn I guess some recent change in expl3 made \dim_to_unit unknown? – Gonzalo Medina Aug 13 '15 at 17:26
  • @GonzaloMedina Yes, there has been a change. Thanks for noting. – egreg Aug 13 '15 at 17:30
4

In order to set the canvas size, you must pass a size option to gnuplot. I don't know a way to cut off the margins automatically, this would require a crop option for the gnuplottex package.

So, the solution I can present you is to set the size manually, and adjust the margins from within gnuplot with e.g. set lmargin and set rmargin. You can also use the pdf option for the cairolatex terminal, then you don't need epstopdf:

\documentclass{article}

\usepackage[cleanup]{gnuplottex}
\usepackage{graphicx}
\usepackage{lipsum}

\begin{document}
\lipsum[1]

\noindent\begin{minipage}{\textwidth}%
  \centering  
  \begin{gnuplot}[terminal=cairolatex, terminaloptions={pdf size 12cm,4cm}]
     set lmargin 4.5
     set rmargin 0.1
     plot [0:2*pi] sin(x) with lines linewidth 2 title 'Sine'
  \end{gnuplot}
\end{minipage}%

\lipsum[1]
\end{document}

This gives:

enter image description here

Christoph
  • 16,593
  • 1
  • 23
  • 47
0

I'd just use

\begin{gnuplot}[terminal=cairolatex]
set size 1,0.6
plot [0:2*pi] sin(x) with lines linewidth 2 title 'Sine'
\end{gnuplot}

Maybe needs some fiddling at the beginning, but I think it's easier in the end than messing around with margins.

John
  • 1,495