6

So, I'm making figures using pgfplots and I have a problem: They don't centre on the page well. I'd rather stick to article for ease of printing and so I can have multiple figure in the same document (If I have multiple spectra of the same compound, for example, it would be nice to have them all in one place).

However, when I compile it, the figure doesn't go all the way our to the right margin, which is quite annoying. How do I scale the figure to automatically use the whole page, but not have the axis lables go off the page or such?

I have a MWE that I used a tiny subset of my data in. I also pared it down (I normally use an external file for the data) and removed the labels and such to make it minimal):

\documentclass[letterpaper]{article}
\usepackage[margin=0.5 in,landscape]{geometry}
\pagestyle{empty}

%Graphics stuff here
\usepackage{pgfplots} %For graphing data
\pgfplotsset
  {
    compat                   = newest,
    every tick/.append style = thin,
    width= 0.95 \textwidth,
    height= 0.95\textheight
  }
\pgfkeys{/pgf/number format/set thousands separator = }

%Science stuff here
\usepackage[]{siunitx} %Adds si units and others by name- See the manual.
\sisetup{mode = text}

\begin{document}
\begin{tikzpicture}
\begin{axis}
    [
      x dir               = reverse,
      xlabel              = Frequency (\si{\per\centi\metre}),
      title                 = Demo,
      xticklabel style      =   {rotate=270},
      yticklabels           = {},
    ]
\addplot[color = black, mark = none]
coordinates {
( 3.983730e+003, 9.824165e+001 )
( 3.984213e+003, 9.854189e+001 )
( 3.984695e+003, 9.890483e+001 )
( 3.985177e+003, 9.878275e+001 )
( 3.985659e+003, 9.859460e+001 )
( 3.986141e+003, 9.835152e+001 )
( 3.986623e+003, 9.794798e+001 )
( 3.987105e+003, 9.803477e+001 )
( 3.987587e+003, 9.864641e+001 )
( 3.988070e+003, 9.895673e+001 )
( 3.988552e+003, 9.910266e+001 )
( 3.989034e+003, 9.866454e+001 )
( 3.989516e+003, 9.837458e+001 )
( 3.989998e+003, 9.857204e+001 )
( 3.990480e+003, 9.883611e+001 )
( 3.990962e+003, 9.891921e+001 )
( 3.991444e+003, 9.846350e+001 )
( 3.991927e+003, 9.804715e+001 )
( 3.992409e+003, 9.815513e+001 )
( 3.992891e+003, 9.844558e+001 )
( 3.993373e+003, 9.842175e+001 )
( 3.993855e+003, 9.843822e+001 )
( 3.994337e+003, 9.828293e+001 )
( 3.994819e+003, 9.791080e+001 )
( 3.995301e+003, 9.774442e+001 )
( 3.995783e+003, 9.783126e+001 )
( 3.996266e+003, 9.788599e+001 )
( 3.996748e+003, 9.826096e+001 )
( 3.997230e+003, 9.857933e+001 )
( 3.997712e+003, 9.843895e+001 )
( 3.998194e+003, 9.839955e+001 )
( 3.998676e+003, 9.863584e+001 )
( 3.999158e+003, 9.872655e+001 )
( 3.999640e+003, 9.836100e+001 )
( 4.000123e+003, 9.836080e+001 )};
\end{axis}
\end{tikzpicture}

\end{document}
  • You have width=0.95\textwidth. Reove 0.95. Then it will span full text width. You may also make margins 0. – user11232 Feb 25 '14 at 0:50
  • Some thoughts: You could make the data sit in a separate file instead of copy and paste so that it will help future iterations if any and use Tikz externalization to speed up for multiple figures and may be use \includegraphics[]{} to scale the figure to the fullest. – texenthusiast Feb 25 '14 at 3:46
  • @texenthusiast I normally keep it as a external file, then import the data, however, I wanted a single file for the MWE to keep things easy to compile. – Canageek Feb 26 '14 at 20:14
  • @Canageek just for info: one might use filecontents package Ex: tex.stackexchange.com/a/44573/15717 – texenthusiast Feb 26 '14 at 20:24
  • @texenthusiast I already had the way of setting it up with internal coordinates written (From before the chemistry saint Joseph Wright taught me how to make the data external), and I figured it wouldn't change anything with the answer just to use that. I'll keep it in mind for future though. – Canageek Feb 27 '14 at 23:42
7

Perhaps putting the tikz in a centered zero-width box is what you are looking for. EDITED to place \centering in a \par-ended group, thanks to suggestion by cfr.

\documentclass[letterpaper]{article}
\usepackage[margin=0.5 in,landscape]{geometry}
\pagestyle{empty}

%Graphics stuff here
\usepackage{pgfplots} %For graphing data
\pgfplotsset
  {
    compat                   = newest,
    every tick/.append style = thin,
    width= .95 \textwidth,
    height= .95\textheight
  }
\pgfkeys{/pgf/number format/set thousands separator = }

%Science stuff here
\usepackage[]{siunitx} %Adds si units and others by name- See the manual.
\sisetup{mode = text}

\begin{document}
{\centering\makebox[0pt]{
\begin{tikzpicture}
\begin{axis}
    [
      x dir               = reverse,
      xlabel              = Frequency (\si{\per\centi\metre}),
      title                 = Demo,
      xticklabel style      =   {rotate=270},
      yticklabels           = {},
    ]
\addplot[color = black, mark = none]
coordinates {
( 3.983730e+003, 9.824165e+001 )
( 3.984213e+003, 9.854189e+001 )
( 3.984695e+003, 9.890483e+001 )
( 3.985177e+003, 9.878275e+001 )
( 3.985659e+003, 9.859460e+001 )
( 3.986141e+003, 9.835152e+001 )
( 3.986623e+003, 9.794798e+001 )
( 3.987105e+003, 9.803477e+001 )
( 3.987587e+003, 9.864641e+001 )
( 3.988070e+003, 9.895673e+001 )
( 3.988552e+003, 9.910266e+001 )
( 3.989034e+003, 9.866454e+001 )
( 3.989516e+003, 9.837458e+001 )
( 3.989998e+003, 9.857204e+001 )
( 3.990480e+003, 9.883611e+001 )
( 3.990962e+003, 9.891921e+001 )
( 3.991444e+003, 9.846350e+001 )
( 3.991927e+003, 9.804715e+001 )
( 3.992409e+003, 9.815513e+001 )
( 3.992891e+003, 9.844558e+001 )
( 3.993373e+003, 9.842175e+001 )
( 3.993855e+003, 9.843822e+001 )
( 3.994337e+003, 9.828293e+001 )
( 3.994819e+003, 9.791080e+001 )
( 3.995301e+003, 9.774442e+001 )
( 3.995783e+003, 9.783126e+001 )
( 3.996266e+003, 9.788599e+001 )
( 3.996748e+003, 9.826096e+001 )
( 3.997230e+003, 9.857933e+001 )
( 3.997712e+003, 9.843895e+001 )
( 3.998194e+003, 9.839955e+001 )
( 3.998676e+003, 9.863584e+001 )
( 3.999158e+003, 9.872655e+001 )
( 3.999640e+003, 9.836100e+001 )
( 4.000123e+003, 9.836080e+001 )};
\end{axis}
\end{tikzpicture}
}\par}
\end{document}

enter image description here

It can be made slightly bigger by changing

\pgfplotsset
  {
    compat                   = newest,
    every tick/.append style = thin,
    width= .95 \textwidth,
    height= .95\textheight
  }

and

\centering\makebox[0pt]{

to the following:

\pgfplotsset
  {
    compat                   = newest,
    every tick/.append style = thin,
    width= .99\textwidth,
    height=.99\textheight
  }

and

\centering\makebox[0pt]{\raisebox{-.99\textheight}{\smash{%

and adding 2 extra closing braces before the \end{document}.

enter image description here

| improve this answer | |
  • (+1) But shouldn't \centering be grouped so it doesn't affect subsequent material? – cfr Jan 17 '15 at 17:09
  • 1
    @cfr Thanks for the reminder. Edited per your suggestion. – Steven B. Segletes Jan 17 '15 at 20:40

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.