[Tex/LaTex] How to change the aspect ratio of gnuplot output

gnuplotgraphsscaling

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

Best Answer

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