1

In order to gain some horizontal space (e.g. to put two plots next to each other) and because this is the least ugly solution for that I would like to be able to put the axis line of a pgfplot exactly on the border where text would begin (or end) in a line. (in the MWE below: it should lie directly on the frame of the text area). Shifting them manually by trying out appropriate values (as done in the MWE) is not the ideal solution.

\documentclass{article}
\usepackage{showframe}
\usepackage{pgfplots}

\newcommand{\myfig}{
    \begin{tikzpicture}
    \begin{axis}[height=5cm,width=7cm]
    \addplot {2^x};
    \end{axis}
    \end{tikzpicture}
    }

\begin{document}
\myfig % too much space on the left

\hspace{-12.5mm}\myfig % correct (but manual) placement

\flushright
\myfig % too much space on the right

\myfig\hspace*{-3.3mm} % correct (but manual) placement
\end{document}

MWE output

I know I must work with anchors but later I am working in floating environments and cannot give an exact (vertical) position related to the page. Who knows how to do it?

2

trim axis left and trim axis right is what you're looking for. These options can be added to the arguments of the tikzpicture (not the axis), and changes the bounding box so that everything left (right) of current axis.south west (current axis.south east) is trimmed away.

Note that in your example I also added some % in the \myfig macro to avoid spurious spaces (see e.g. Why the end-of-line % in macro definitions?), and I added a \noindent to remove the standard paragraph indent.

enter image description here

\documentclass{article}
\usepackage{showframe}
\usepackage{pgfplots}

\newcommand{\myfig}[1]{%
    \begin{tikzpicture}[#1]
    \begin{axis}[height=5cm,width=7cm]
    \addplot {2^x};
    \end{axis}
    \end{tikzpicture}%
    }

\begin{document}
\noindent\myfig{trim axis left}


\flushright
\myfig{trim axis right}
\end{document}
  • way easier than I expected, very nice! I know about the % trick and my final document has no indentaion and I didn't care about either of those for my example, but an extra thanks for even thinking about that! – riddleculous Mar 30 '16 at 19:14

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.