Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
That solution would work if you produce a single file containing the image produced by the tikz. –  Sigur Jan 22 '14 at 0:15
1  
@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 0: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 0:26
    
\resizebox works. You should use it as follows: \resizebox{\textwidth}{!}{\begin{tikzpicture}...\end{tikzpicture}} –  Herr K. Jan 22 '14 at 0:30
    
The answers to this question, tex.stackexchange.com/q/17293/18228, may be helpful. –  Herr K. Jan 22 '14 at 0:34

2 Answers 2

up vote 18 down vote accepted

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

share|improve this answer
    
If the fonts are not scaled, then unfortunately I cannot use this method. –  user41709 Jan 22 '14 at 0:43
2  
@user41709 Scaling fonts is a bad idea. Any way it is your document. You can use \resizebox then. –  Harish Kumar Jan 22 '14 at 0: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 0:46
    
@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 0:50
    
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 0:59

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.