You can set the bounding box of a tikzpicture
explicitly by using \useasboundingbox
or more TikZ'ish:
\path[use as bounding box] (0,0) rectangle (10,10); % adjust to fit
in your tikzpicture
.
You will have to provide some more information if the above is not what you want. As stated above, the way to control the size of a tikzpicture
is to set the bounding box manually if you are not happy with what TikZ is calculating. To quote the pgfmanual (section Establishing a bounding Box):
pgf is reasonably good at keeping track of the size of your picture and reserving just the right amount of
space for it in the main document. However, in some cases you may want to say things like "do not count
this for the picture size" or "the picture is actually a little large." For this you can use the option use as
bounding box or the command \useasboundingbox
, which is just a shorthand for \path[use as bounding
box]
.
You might also try using the external library
\usetikzlibrary{external}
\tikzexternalize
this offers some additional functionality on top of the
\beginpgfgraphicnamed ...\endpgfgraphicnamed.
Alternatively, the preview
package offers a way to extract tightly cropped parts of a document
\usepackage[active,pdftex,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
The MWE works just fine using the externalisation library. Converting the syntax to use pgfkeys
actually solves the scoping problem since keys are local to the current scope. Thus you can pass the keys and values as an option to the matlabfig
command and they will be used only internally to that command.
I'm only a newcomer to the world of of pgfkeys
so there may be better ways of implementing this, but here's a conversion of your macros into pgfkeys
language. I only changed one thing in the input syntax: the ranges are specified as x range=0:6
instead of x range={0}{6}
.
When putting this in to an environment, the problem is that the externalisation library looks for \end{tikzpicture}
without doing any expansion. In an ordinary environment, this doesn't work because the \end{tikzpicture}
is hidden behind a \end{myenvironment}
. So we have to have a method whereby the \end{myenvironment}
is expanded at the same time as the \begin{tikzpicture}
. Fortunately such a method exists and is to use the environ package. This makes environments behave a little like commands.
\documentclass{article}
\usepackage{environ}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize
\tikzset{external/force remake=true}
\usepackage{pgfplots}
\def\figurewidth{7.5cm}
\def\figureheight{4cm}
\pgfkeys{/matlab plot/.cd,
title/.default={\null},
title height/.default={0cm},
x label/.default={\null},
x label height/.default={0},
x range/.code args={#1:#2}{\pgfkeyssetvalue{/matlab plot/x range}{xmin=#1,xmax=#2}},
y range/.code args={#1:#2}{\pgfkeyssetvalue{/matlab plot/y range}{ymin=#1,ymax=#2}},
legend/.code args={#1#2}{\pgfkeyssetvalue{/matlab plot/legend}{legend entries=#1,legend pos=#2}},
title/.code={%
\pgfkeyssetvalue{/matlab plot/title}{#1}%
\pgfkeyssetvalue{/matlab plot/title height}{0.45cm}},
x label/.code={%
\pgfkeyssetvalue{/matlab plot/x label}{#1}%
\pgfkeyssetvalue{/matlab plot/x label height}{-0.35}},
caption/.code={%
\pgfkeyssetvalue{/matlab plot/caption}{\caption{#1}}},
label/.code={\pgfkeyssetvalue{/matlab plot/caption}{\label{#1}}}%
}
% Initialise
\pgfkeyssetvalue{/matlab plot/caption}{}
\pgfkeyssetvalue{/matlab plot/label}{}
\pgfkeyssetvalue{/matlab plot/x label}{}
\pgfkeyssetvalue{/matlab plot/y label}{}
\pgfkeyssetvalue{/matlab plot/extra}{}
\pgfkeyssetvalue{/matlab plot/legend}{}
\pgfkeyssetvalue{/matlab plot/x range}{}
\pgfkeyssetvalue{/matlab plot/x range}{}
\pgfkeyssetvalue{/matlab plot/y range}{}
\pgfkeyssetvalue{/matlab plot/y range}{}
\pgfkeyssetvalue{/matlab plot/pos}{}
\def\figysep{1}
\newcommand{\matlabfig}[2][]{%figuur zelf
\begingroup
\pgfkeys{/matlab plot/.cd,#1}
\begin{figure}[\pgfkeysvalueof{/matlab plot/pos}]
\centering
\begin{tikzpicture}[]
% \draw[use as bounding box,draw=none](0,0+\pgfkeysvalueof{x label height})rectangle(\figurewidth,\figureheight+\pgfkeysvalueof{title height});
\begin{axis}[%
view={0}{90},
scale only axis,
width=\figurewidth,
height=\figureheight,
y tick label style={font={\tiny}},
x tick label style={font={\tiny}},
title={\textbf{\textsc{\pgfkeysvalueof{/matlab plot/title}}}},
xlabel={\textbf{\pgfkeysvalueof{/matlab plot/x label}}},
ylabel={\textbf{\pgfkeysvalueof{/matlab plot/y label}}},
\pgfkeysvalueof{/matlab plot/x range},
\pgfkeysvalueof{/matlab plot/y range},
axis on top,
\pgfkeysvalueof{/matlab plot/legend},
\pgfkeysvalueof{/matlab plot/extra}
minor tick num=1
]
\input{#2}
\end{axis}
\end{tikzpicture}
\pgfkeysvalueof{/matlab plot/caption}
\pgfkeysvalueof{/matlab plot/label}
\end{figure}
\endgroup}
\NewEnviron{MatlabFig}[1][]{%
\begingroup
\pgfkeys{/matlab plot/.cd,#1}
\begin{figure}[\pgfkeysvalueof{/matlab plot/pos}]
\centering
\begin{tikzpicture}
% \draw[use as bounding box,draw=none](0,0+\pgfkeysvalueof{x label height})rectangle(\figurewidth,\figureheight+\pgfkeysvalueof{title height});
\begin{axis}[%
view={0}{90},
scale only axis,
width=\figurewidth,
height=\figureheight,
y tick label style={font={\tiny}},
x tick label style={font={\tiny}},
title={\textbf{\textsc{\pgfkeysvalueof{/matlab plot/title}}}},
xlabel={\textbf{\pgfkeysvalueof{/matlab plot/x label}}},
ylabel={\textbf{\pgfkeysvalueof{/matlab plot/y label}}},
\pgfkeysvalueof{/matlab plot/x range},
\pgfkeysvalueof{/matlab plot/y range},
axis on top,
\pgfkeysvalueof{/matlab plot/legend},
\pgfkeysvalueof{/matlab plot/extra}
minor tick num=1
]
\BODY
\end{axis}
\end{tikzpicture}
\pgfkeysvalueof{/matlab plot/caption}
\pgfkeysvalueof{/matlab plot/label}
\end{figure}
\endgroup}
\pgfplotsset{
every tick/.style={thin,color=black},
every axis legend/.append style={nodes={right}},
every axis legend/.append style={font=\tiny},
compat=newest
}
\tikzstyle{every node}=[font=\scriptsize]
\begin{document}
%%
\matlabfig[
caption=an example fig,
label=fig:example,
title=example,
x label=$x$ axis,
y label=$y$ axis,
legend={data.tikz}{north west},
x range=0:6,
y range=0:5,
]{tikzextkeys_data.tikz}
%%
%another fig, but as you can see all was resetted
\matlabfig{tikzextkeys_data.tikz}
\begin{MatlabFig}[
caption=an example fig,
label=fig:example,
title=example,
x label=$x$ axis,
y label=$y$ axis,
legend={data.tikz}{north west},
x range=0:6,
y range=0:5,
]
\input{tikzextkeys_data.tikz}
\end{MatlabFig}
\end{document}
(NB In this code, something is going wrong when I uncomment the bounding box line. It's to do with getting the lengths and values of the keys to interact properly.)
Best Answer
If you have a recent version of tikz, there is a handy command that will do this for you.
If you go here: http://www.texample.net/tikz/builds/ you can download a up-to-date version of tikz. The documentation has instructions on how to implement the externalization in section 4.31 Externalization (in the current documentation this is page 340). The basic setup is explained in an example.
I use this for my own research and it is extremely easy. You can find a file I have implemented this in here more-ar-quivers.tex.