# Problem with scaling used in \includegraphics

I have a question on scaling a graph when the graph is included using \includegraphics. A code snippet is below.

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{tikzscale}

\usetikzlibrary{external}
\tikzexternalize
%\tikzset{external/mode=list and make}
%\tikzset{external/check=diff}
\tikzset{external/force remake}
%\tikzsetexternalprefix{figure-build/, up to date check=md5, force remake}

\pgfplotsset{compat=1.8}
%\pgfplotsset{compat=newest} % necessary for new features

\usepackage{filecontents}

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{0.5cm}

\begin{filecontents*}{testCross1.tikz}
\begin{tikzpicture}
\draw (0,0) -- (1,1) (0,1) -- (1,0);
\end{tikzpicture}
\end{filecontents*}

\begin{filecontents*}{testCross2.tikz}
\begin{tikzpicture}[xscale=3.0,yscale=2.0]
\draw (0,0) -- (1,1) (0,1) -- (1,0);
\end{tikzpicture}
\end{filecontents*}

\begin{figure}[bth]
\begin{tabular}{p{3in}}
\includegraphics[]{testCross1} \\
\includegraphics[width=0.35\linewidth,height=0.2\linewidth]{testCross1} \\
\includegraphics[]{testCross2}
\end{tabular}
\caption{\small{Plot.}}
\end{figure}

\end{document}


This code snippet is compiled by

pdflatex -shell-escape main.tex


I have tried three ways of scaling a graph.

First, I used \includegraphics[width=0.35\linewidth,height=0.2\linewidth]{testCross1}, but it does not work. The graph stays the same size as the original graph.

Second, I specified the scaling together the declaration of the graph as below.

\begin{tikzpicture}[xscale=3.0,yscale=2.0]
\draw (0,0) -- (1,1) (0,1) -- (1,0);
\end{tikzpicture}


It works. But, I really want to be able to dynamically specify the scaling parameters without changing the specification of the tikzpicture.

So, I tried to scale the graph by the following code:

\includegraphics[xscale=3.0,yscale=2.0]{testCross1}

But, an error is reported in compilation. The error message in the .log file says that

! Package pgfkeys Error: I do not know the key '/tikzscale/xscale' and I am going to ignore it. Perhaps you misspelled it.


Could you help me to show the two ways that a graph can be resized through the following declarations?

\includegraphics[xscale=3.0,yscale=2.0]{testCross1}


and

\includegraphics[width=0.35\linewidth,height=0.2\linewidth]{testCross1}

-
Try with \scalebox (see graphicx documentation) around \includegraphics. –  JLDiaz Aug 23 '13 at 19:43
oh that's bizarre it seems that the package takes over the includegrapics optional argument so that its keys are handled by tikz so its normal keys like scale are unknown. As JLDiaz suggest you may be better to handle the scaling directly via \scalebox –  David Carlisle Aug 23 '13 at 20:45
Thanks. The method of using \scalebox works. However, this method is not very clean because the fonts of the x/y labels ugly displayed for certain values of the scales. Meanwhile, I want to confirm that the options used by \includegraphics can not be passed into the tikzpicture environment. Am I on the right track? –  Novice User Aug 23 '13 at 21:17

The package is doing something very strange with the includegraphics arguments:-)

    \resizebox{\linewidth}{2cm}{\includegraphics{testCross1}}\\


generates an error, as does

    \resizebox{\linewidth}{2cm}{\mbox{\includegraphics{testCross1}}}\\


but bizarrely this works:

    \resizebox{\linewidth}{2cm}{\fbox{\includegraphics{testCross1}}}\\


and you can hide the fbox rules by:

    \resizebox{\linewidth}{2cm}{\setlength\fboxsep{0pt}\fbox{\includegraphics{testCross1}}}\\


The above image is made with:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{tikzscale}

\usetikzlibrary{external}
\tikzexternalize
%\tikzset{external/mode=list and make}
%\tikzset{external/check=diff}
\tikzset{external/force remake}
%\tikzsetexternalprefix{figure-build/, up to date check=md5, force remake}

\pgfplotsset{compat=1.8}
%\pgfplotsset{compat=newest} % necessary for new features
\usepackage{filecontents}

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{0.5cm}

\begin{filecontents*}{testCross1.tikz}
\begin{tikzpicture}
\draw (0,0) -- (1,1) (0,1) -- (1,0);
\end{tikzpicture}
\end{filecontents*}

\begin{filecontents*}{testCross2.tikz}
\begin{tikzpicture}[xscale=3.0,yscale=2.0]
\draw (0,0) -- (1,1) (0,1) -- (1,0);
\end{tikzpicture}
\end{filecontents*}

\begin{figure}[bth]
\begin{tabular}{p{3in}}
\resizebox{\linewidth}{2cm}{\setlength\fboxsep{0pt}\fbox{\includegraphics{testCross1}}}\\
\end{tabular}
\caption{\small{Plot.}}
\end{figure}

\end{document}

-