2

If I have a nice plot in matlab, then I save it as a tikz standalone file

matlab2tikz('myimage.tikz','standalone',true)

and I create a pdf in my terminal with

pdflatex myimage.tikz

to include the pdf in my document instead of the tikz code to speed up the compiler time. This is a typical result: enter image description here The problem is that I would like to center only the plot in my thesis and not the plot together with the y-label. enter image description here Thus, I would need to add in my pdf documet of the single plot (the top one) a right margin which has the same size as the y-label margin. How can I get this?

Note: I found a similar problem at pgfplots and figure centering but if I use

\begin{tikzpicture}[trim axis left, trim axis right]

then the y-label is simply deleted in my outcome pdf. Thus I need a different solution for this problem.

EDIT: Here is the actual code from myimage.tikz:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{grffile}
\pgfplotsset{compat=newest}
\usetikzlibrary{plotmarks}
\usepackage{amsmath}

\begin{document}
\begin{tikzpicture}

\begin{axis}[%
width=4.52083333333333in,
height=1.5in,
scale only axis,
xmin=-3,
xmax=3,
xlabel={x},
ymin=0,
ymax=25,
ylabel={V(x)},
enlarge x limits=0.15,
enlarge y limits={rel=0.01}
]
\addplot [color=blue,solid,forget plot,line width=2pt]
  table[row sep=crcr]{%
-3  25\\
-2.983  23.993235127521\\
-2.951  22.169039976801\\
-2.688  10.402843918336\\
-2.598  7.560322156816\\
-2.156  0.420339568896001\\
-2.074  0.0908877785759996\\
-1.519  2.865026784321\\
-1.417  3.968506236321\\
-1.261  5.807516794641\\
-0.663  12.676668905761\\
-0.442  14.475255092496\\
-0.179  15.744698625681\\
0.0939999999999999  15.929390074896\\
0.323   15.176252540241\\
0.52    13.90991616\\
0.708   12.241153597696\\
0.947   9.629794382481\\
1   9\\
1.061   8.261479769841\\
1.323   5.061019608241\\
1.641   1.708560080161\\
1.864   0.276154454016\\
2.136   0.316401750016001\\
2.432   3.665785061376\\
2.653   9.231929251281\\
2.929   20.967616479681\\
3   25\\
};
\end{axis}
\end{tikzpicture}%
\end{document}

I would be interest to understand the steps to find out how much space I need to add to the right and how I have to add it. Its okay for me to adjust it for every single image again.

Adam
  • 1,268
  • 2
    Can you provide us the actual image (or a similar one, having the same essential characteristics) allowing use to play with it? – Gonzalo Medina Jul 26 '15 at 23:34
  • If you use \input you can centre the image appropriately. If you combine this with externalisation and/or standalone or similar, then you can arrange things so that the image is compiled only on the first run, unless the source is altered later. Of course, if the shift you need is always the same, you can easily adjust the placement. But if it varies from one image to the next, you cannot recover that information from the compiled PDF so you'd need to specify it for each case. – cfr Jul 27 '15 at 01:00
  • First, put it into an \fbox to see that the dimensions are. Then either add the difference to the right or subtract from the left. . – John Kormylo Jul 27 '15 at 01:45
  • We really need the code myimage.tikz, but may be you can try with \hspace{-length} justbefore \includegraphics – touhami Jul 27 '15 at 08:29
  • @GonzaloMedina I have provided the tikz code – Adam Jul 27 '15 at 09:58
  • @touhami I have provided the tikz code – Adam Jul 27 '15 at 09:58
  • @JohnKormylo Could you explain this in more detail? I guess I can add right space with \hspace{..}? How does fbox tell me the dimensions? – Adam Jul 27 '15 at 10:00
  • @cfr Thank you. But I rather like to have a small code for each image that I can compile separately. In this way, I can adjust my image and compile it very quickly without compiling my whole thesis. – Adam Jul 27 '15 at 10:07
  • try to add \node at (5in,0) {}; just after \end{axis} – touhami Jul 27 '15 at 10:35
  • @Adam That is precisely the point of standalone. You can compile the image separately or as part of the whole thing. – cfr Jul 27 '15 at 12:11

1 Answers1

2

You could use trim axis left and trim axis right as options for the tikzpicture and additionally border={45pt 0pt} as option for the standalone class. The first value inside the class option is the space added to the left and the right border of resulting bounding box of the image. It must be large enough to show the y-label but it could be also larger. In the example I use 100pt and I change the width of the axis to 3.5in.

\documentclass[tikz,margin={100pt 0pt}]{standalone}
\usepackage{pgfplots}
\usepackage{grffile}
\pgfplotsset{compat=newest}
\usetikzlibrary{plotmarks}
\usepackage{amsmath}

\begin{document}
\begin{tikzpicture}[trim axis left,trim axis right]

\begin{axis}[%
width=3.5in,
height=1.5in,
scale only axis,
xmin=-3,
xmax=3,
xlabel={x},
ymin=0,
ymax=25,
ylabel={V(x)},
enlarge x limits=0.15,
enlarge y limits={rel=0.01}
]
\addplot [color=blue,solid,forget plot,line width=2pt]
  table[row sep=crcr]{%
-3  25\\
-2.983  23.993235127521\\
-2.951  22.169039976801\\
-2.688  10.402843918336\\
-2.598  7.560322156816\\
-2.156  0.420339568896001\\
-2.074  0.0908877785759996\\
-1.519  2.865026784321\\
-1.417  3.968506236321\\
-1.261  5.807516794641\\
-0.663  12.676668905761\\
-0.442  14.475255092496\\
-0.179  15.744698625681\\
0.0939999999999999  15.929390074896\\
0.323   15.176252540241\\
0.52    13.90991616\\
0.708   12.241153597696\\
0.947   9.629794382481\\
1   9\\
1.061   8.261479769841\\
1.323   5.061019608241\\
1.641   1.708560080161\\
1.864   0.276154454016\\
2.136   0.316401750016001\\
2.432   3.665785061376\\
2.653   9.231929251281\\
2.929   20.967616479681\\
3   25\\
};
\end{axis}
\end{tikzpicture}
\end{document}

results in

enter image description here

Use \makebox[\linewidth]{\includegraphics[<options>]{myimage}} to insert this myimage.pdf in your main document.

\documentclass{article}
\usepackage{lipsum}% for dummy text
\usepackage{showframe}% to show the page layout
\usepackage{graphicx}
\begin{document}
\begin{figure}%
\centering
\makebox[\linewidth]{\includegraphics{myimage}}%
\caption{Caption}\label{fig:myimage}%
\end{figure}
\lipsum
\end{document}

results in

enter image description here

esdd
  • 85,675