6

I produce a lot of plots in Matlab which I export to TikZ with matlab2tikz. In matlab2tikz I can specify the plot width and height, for instance. This works out quite nicely, but as I write my document I see that I should have changed the width and height to different values for several plots. Now, this is of course something I either could do in the matlab2tikz call or directly in the .tex file output from matlab2tikz. I have quite significant amounts of plots and most of them are created in "automated" procedures, so it would be nice if I could pass an argument to \input (or similar) which could manipulate the width and/or height of the plot. Something like:

\input[w=0.4,h=0.2]{plotfile}

and have something like this in plotfile:

\begin{axis}[%
width=w\textwidth,
height=h\textwidth,
]

Is there a solution to this?

T. Verron
  • 13,552
Holene
  • 6,920
  • 4
    you don't want to use \include -- that always starts a new page, and is most appropriate for pulling in new chapters. – barbara beeton Apr 30 '13 at 14:57
  • 1
    Take a look at the tikzscale package. It’s written for scaling the picture to the exact size specified (unlike the width and height option of pgfplots which doesn’t factor in the labels). – Qrrbrbirlbel Apr 30 '13 at 15:47
  • @Qrrbrbirlbel : As far as I know, not rescaling the label should be the wanted result, for typographically-consistent document. – T. Verron Apr 30 '13 at 15:56
  • 1
    @T.Verron Nobody said anything about re-scaling the label. In fact, that is one major argument for using tikzscale. pgfplots doesn’t know the exact dimensions of the labels so it doesn’t get the plot’s resulting width/height precisely as specified. See pgfplots: how can I scale to text width? – Qrrbrbirlbel Apr 30 '13 at 17:26
  • @Qrrbrbirlbel : Oh, ok, my misunderstanding, sorry. – T. Verron Apr 30 '13 at 17:31

2 Answers2

7

I haven't tested it with \include statement but you can localize your axis options and let them executed after the axis options.

EDIT: Oh and of course your \input is going to replace the tikzpictures below.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}

{
\pgfplotsset{execute at begin axis={\pgfplotsset{width=3cm}}} %<-- Only inside this group
\begin{tikzpicture}
\begin{axis}[width=10cm,domain=-360:360]
    \addplot {sin(x)};
\end{axis}
\end{tikzpicture}
}

\begin{tikzpicture}
\begin{axis}[width=10cm,domain=-360:360]
    \addplot {sin(x)};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

percusse
  • 157,807
  • So this allows you to set some kind of global width which you can limit to a certain scope? – Holene May 01 '13 at 06:44
  • @Holene It forces an option to be executed as the last one as if we have added manually to each axis environment. Scoping limits its effect. – percusse May 02 '13 at 07:43
5

How about defining it this way?

\newlength{\figurewidth}
\newlength{\figureheight}

\newcommand{\myinputaux}[2][1]{%
  \def\myheight{#1}
  \setlength\figureheight{\myheight\textwidth}
  \setlength\figurewidth{\mywidth\textwidth} 
  \input{#2}}

\newcommand{\myinput}[1][1]{%
  \def\mywidth{#1}
  \myinputaux}

You would then call this function like this:

\myinput[0.4][0.2]{plotfile} % the first optional is the width, the second is the height

and execute matlab2tikz with

matlab2tikz( 'plotfile', 'height', '\figureheight', 'width', '\figurewidth' );

Note that this answer is purely theoretical, based on the readme for matlab2tikz. I couldn't test it "for real".

egreg
  • 1,121,712
T. Verron
  • 13,552
  • Oooh, that would be a nifty solution. However, I get loads of errors when trying to compile with your solution, missing \begin{document} at the line of \input{#2}} being one of them... – Holene Apr 30 '13 at 15:23
  • Absolutely no idea why I got that error message. Made a new, stripped document, and it works. NICE DUDE! – Holene Apr 30 '13 at 15:32
  • @egreg's edit (thanks to him!) might be what fixed your mysterious problem. I had forgotten a backslash in the definition of \myinputaux. – T. Verron Apr 30 '13 at 15:59