37

A user asked a question about scaling a figure to a factor of \textwidth and got an answer like this:

\documentclass{article}
\usepackage{graphicx}
\begin{document}
  \includegraphics[width=0.5\textwidth]{file}
\end{document}

however, I'm not sure how to apply this to my document:

\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\begin{document}
  \begin{figure}[!h]
    \centering
    \begin{tikzpicture}
      %
    \end{tikzpicture}
    \caption{This figure has a width which is a factor of \\textwidt}
  \end{figure}
\end{document}

I've tried

\begin{figure}[!h, width=\textwidth]

but that didn't work.

user41709
  • 519
  • That solution would work if you produce a single file containing the image produced by the tikz. – Sigur Jan 22 '14 at 00:15
  • 2
    @sigur yes but I have dozens of tikz figures and I don't want the source for my document to be spread across dozens of files that I need to keep, backup, and maintain constantly. There must be a way to scale it. \resizebox[\textwidt][!]{...} doesn't seem to work either. – user41709 Jan 22 '14 at 00:25
  • In this case you have to scale the tikz using \begin{tikzpicture}[scale=.7] for example. But this will change the scale of coordinates. – Sigur Jan 22 '14 at 00:26
  • \resizebox works. You should use it as follows: \resizebox{\textwidth}{!}{\begin{tikzpicture}...\end{tikzpicture}} – Herr K. Jan 22 '14 at 00:30
  • The answers to this question, http://tex.stackexchange.com/q/17293/18228, may be helpful. – Herr K. Jan 22 '14 at 00:34
  • \resizebox[\textwidt][!]{...} will scale fonts too. –  Jan 22 '14 at 00:37

2 Answers2

39

You need tikzscale package. Save the contents

\begin{tikzpicture}
  \draw (0,0) circle (2cm);
  \node at (0,0) {Me};
\end{tikzpicture}

as myfig.tikz (say) and use \includegraphics

Code:

\documentclass{article}
\usepackage{tikz}
\usepackage{tikzscale}
\usepackage{filecontents}    %% only for this demo
\begin{filecontents*}{myfig.tikz}
  \begin{tikzpicture}
      \draw (0,0) circle (2cm);
      \node at (0,0) {Me};
  \end{tikzpicture}
\end{filecontents*}
\begin{document}
  \begin{figure}[htb]
    \centering
    \includegraphics[width=0.5\textwidth]{myfig.tikz}
    \caption{This figure has a width which is a factor of text width}
  \end{figure}

  \begin{figure}[htb]
    \centering
    \includegraphics[width=0.2\textwidth]{myfig.tikz}
    \caption{This figure has a width which is a factor of text width}
  \end{figure}
\end{document}

enter image description here

Unlike \resizebox, the fonts are not scaled in-appropriately. If you want to scale fonts too, use \begin{tikzpicture}[transform shape] instead of \begin{tikzpicture}.

With \resizebox from graphicx:

\documentclass{article}
\usepackage{tikz}

\begin{document}
  \begin{figure}[htb]
    \centering
    \resizebox{0.5\textwidth}{!}{%
    \begin{tikzpicture}
      \draw (0,0) circle (2cm);
      \node at (0,0) {Me};
  \end{tikzpicture}
    }%
    \caption{This figure has a width which is a factor of text width}
  \end{figure}

  \begin{figure}[htb]
    \centering
    \resizebox{0.2\textwidth}{!}{%
    \begin{tikzpicture}
      \draw (0,0) circle (2cm);
      \node at (0,0) {Me};
  \end{tikzpicture}
    }%
    \caption{This figure has a width which is a factor of text width}
  \end{figure}
\end{document}

enter image description here

Flow
  • 1,013
  • 1
    If the fonts are not scaled, then unfortunately I cannot use this method. – user41709 Jan 22 '14 at 00:43
  • 3
    @user41709 Scaling fonts is a bad idea. Any way it is your document. You can use \resizebox then. –  Jan 22 '14 at 00:45
  • @user41709: You can still use this method, I think, if you pass the transform shape option to tikzpicture. – Herr K. Jan 22 '14 at 00:46
  • 1
    @HarishKumar The issue is I've spent weeks on all the content and figures for one document, and now for a different conference we need to use a template with a significantly narrower text width. I simply do not have the time to re-create these figures for the larger page margins. – user41709 Jan 22 '14 at 00:50
  • 2
    So my best option is to compile each figure as a separate PDF file, and include them with a scale? That will scale everything correctly? – user41709 Jan 22 '14 at 00:59
8

You may want to consider package adjustbox as well.

\documentclass{article}
\usepackage{graphicx,adjustbox}
\usepackage{tikz}
\begin{document}
\begin{figure}[!h]
  \centering
  \begin{tikzpicture}
    \draw (0,0) rectangle (10,2);
  \end{tikzpicture}
  \caption{This figure uses no factor}
\end{figure}

\begin{figure}[!h]
  \centering
  \begin{adjustbox}{max width=0.5\textwidth}
  \begin{tikzpicture}
    \draw (0,0) rectangle (10,2);
  \end{tikzpicture}
\end{adjustbox}
\caption{This figure uses 0.5 factor}
\end{figure}

\begin{figure}[!h]
  \centering
  \begin{adjustbox}{max width=0.7\textwidth}
  \begin{tikzpicture}
    \draw (0,0) rectangle (10,2);
  \end{tikzpicture}
\end{adjustbox}
\caption{This figure uses factor 0.7}
\end{figure}

\end{document}    

enter image description here

Yossi Gil
  • 15,951