1

I'm fairly new to LaTeX and I'm trying to make a homework template that uses figures generated in LaTeX but when I use TikZ I get a bounding box that is way too large. What is the easiest way to automatically get TikZ to make a tight bounding box?

Here is my code:

\documentclass{article}[12 pt]
\usepackage{amsmath,amssymb, amsthm, pgfplots} % Math packages
\pgfplotsset{compat=1.6}

\usepackage{tikz}

\usetikzlibrary{arrows}

\usepackage[margin=1in]{geometry} % Page settings

\usepackage{fancyhdr} %Used for headers
\usepackage{graphicx}


 \pagestyle{fancy} % Sets page style to fancy
\fancyhf{} % Clears standard header
\lhead{\textbf{Algebra II}} %Subject Name
\chead{\textbf{Name:}\hspace{3 in}}
\rhead{\textbf{Mr. A}}  %Teacher name

\rfoot{Page \thepage}


\begin{document}

\begin{flushright}
\textbf{Period:}  \makebox[0.5 in]{\hrulefill} %Transfer this to header
\textbf{Date:}  \makebox[0.75 in]{\hrulefill} \\[20 pt]
\end{flushright}

\center{\LARGE{\textbf{Intervals, Parent Functions and Domain \& Range}}}

%Create Question command

\center{Directions: Given a representation of an interval, give the other two representations. Below are examples of the three representations} \\

\begin{tikzpicture}
\begin{axis}[
axis y line=none,
axis lines=left,
axis line style={<->},
xmin=-5,
xmax=5,
ymin=0,
ymax=1,
%xlabel=$\mathbb R$,
scatter/classes={
                o={mark=*,draw=black,fill=white},
                c={mark=*,black}
                },
restrict y to domain=0:1,
xtick={-5,-4,...,5},
point meta=explicit symbolic,
 ]
 \addplot[scatter,black,ultra thick] table [y expr=0,meta index=1, header=false] {
 -2 c
 3 c
 };


 \end{axis}

 \end{tikzpicture}





 \end{document}
  • It seems like pgfplot has a default size of bounding box. So you may either set the size by hand: \begin{axis}[width=10cm,height=2cm,...; or clip it using tikzpicture outside. – Symbol 1 Sep 8 '14 at 1:45
  • What do you mean by "using tikzpicture outside"? – 1028 Sep 8 '14 at 1:49
  • In a general tikzpicture you can set the bounding box by hand no matter how big/small its entries are and what they are. – Symbol 1 Sep 8 '14 at 1:53
  • I don't see the too big bounding box. It's just as big as the contents of the tikz picture. Can you elaborate what is too big? – percusse Sep 8 '14 at 5:33
2

You could clip the plot although specifying the bounding box for the picture as Symbol1 suggests is probably better.

Clipping

\documentclass{article}[12 pt]
\usepackage{amsmath,amssymb, amsthm, pgfplots} % Math packages
\pgfplotsset{compat=1.6}

\usepackage{tikz}

\usetikzlibrary{backgrounds}% only to show the bounding box

\usepackage[margin=1in]{geometry} % Page settings

\usepackage{fancyhdr} %Used for headers

\pagestyle{fancy} % Sets page style to fancy
\fancyhf{} % Clears standard header
\lhead{\textbf{Algebra II}} %Subject Name
\chead{\textbf{Name:}\hspace{3 in}}
\rhead{\textbf{Mr. A}}  %Teacher name

\rfoot{Page \thepage}


\begin{document}

  \begin{flushright}
    \textbf{Period:}  \makebox[0.5 in]{\hrulefill} %Transfer this to header
    \textbf{Date:}  \makebox[0.75 in]{\hrulefill} \\[20 pt]
  \end{flushright}

  \begin{center}% center is an environment...
    \LARGE{\textbf{Intervals, Parent Functions and Domain \& Range}}

  %Create Question command

  Directions: Given a representation of an interval, give the other two representations. Below are examples of the three representations.
  \end{center}

  \begin{tikzpicture}[show background rectangle]% only to show (roughly) the bounding box
    \clip (-.5,-1) |- (7.5,1) |- cycle;
    \begin{axis}[
      axis y line=none,
      axis lines=left,
      axis line style={<->},
      xmin=-5,
      xmax=5,
      ymin=0,
      ymax=1,
      %xlabel=$\mathbb R$,
      scatter/classes={
        o={mark=*,draw=black,fill=white},
        c={mark=*,black}
      },
      restrict y to domain=0:1,
      xtick={-5,-4,...,5},
      point meta=explicit symbolic,
      ]
      \addplot[scatter,black,ultra thick] table [y expr=0,meta index=1, header=false] {
        -2 c
        3 c
      };
    \end{axis}
  \end{tikzpicture}

\end{document}

Setting bounding box

Or you can set the bounding box explicitly:

\documentclass{article}[12 pt]
\usepackage{amsmath,amssymb, amsthm, pgfplots} % Math packages
\pgfplotsset{compat=1.6}

\usepackage{tikz}

\usepackage[margin=1in]{geometry} % Page settings

\usepackage{fancyhdr} %Used for headers

\pagestyle{fancy} % Sets page style to fancy
\fancyhf{} % Clears standard header
\lhead{\textbf{Algebra II}} %Subject Name
\chead{\textbf{Name:}\hspace{3 in}}
\rhead{\textbf{Mr. A}}  %Teacher name

\rfoot{Page \thepage}


\begin{document}

  \begin{flushright}
    \textbf{Period:}  \makebox[0.5 in]{\hrulefill} %Transfer this to header
    \textbf{Date:}  \makebox[0.75 in]{\hrulefill} \\[20 pt]
  \end{flushright}

  \begin{center}% center is an environment...
    \LARGE{\textbf{Intervals, Parent Functions and Domain \& Range}}

  %Create Question command

  Directions: Given a representation of an interval, give the other two representations. Below are examples of the three representations.
  \end{center}

  \begin{tikzpicture}
    \draw [use as bounding box] (-5mm,-7.5mm) rectangle (75mm,5mm);
    \begin{axis}[
      axis y line=none,
      axis lines=left,
      axis line style={<->},
      xmin=-5,
      xmax=5,
      ymin=0,
      ymax=1,
      %xlabel=$\mathbb R$,
      scatter/classes={
        o={mark=*,draw=black,fill=white},
        c={mark=*,black}
      },
      restrict y to domain=0:1,
      xtick={-5,-4,...,5},
      point meta=explicit symbolic,
      ]
      \addplot[scatter,black,ultra thick] table [y expr=0,meta index=1, header=false] {
        -2 c
        3 c
      };
    \end{axis}
  \end{tikzpicture}

\end{document}

Saner result

Note that center is an environment and not a command.

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.