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:


and have something like this in plotfile:


Is there a solution to this?

  • 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

How about defining it this way?




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".

  • 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

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.



\pgfplotsset{execute at begin axis={\pgfplotsset{width=3cm}}} %<-- Only inside this group
    \addplot {sin(x)};

    \addplot {sin(x)};

enter image description here

  • So this allows you to set some kind of global width which you can limit to a certain scope? – Holene May 1 '13 at 6: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 2 '13 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.