\includegraphics 中使用的缩放比例存在问题

\includegraphics 中使用的缩放比例存在问题

我对使用 包含图表时缩放图表有疑问\includegraphics。代码片段如下。

\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}

此代码片段由

pdflatex -shell-escape main.tex

我尝试了三种缩放图形的方法。

首先,我使用了\includegraphics[width=0.35\linewidth,height=0.2\linewidth]{testCross1},但它不起作用。图表保持与原始图表相同的大小。

其次,我指定了图形声明的缩放比例,如下所示。

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

它可以工作。但是,我真正希望的是能够动态指定缩放参数,而无需更改 tikzpicture 的规范。

因此,我尝试通过以下代码来缩放图形:

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

但是,编译时会报告错误。.log 文件中的错误消息显示

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

您能帮我展示一下通过以下声明调整图形大小的两种方法吗?

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

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

答案1

该包使用 includegraphics 参数做了一些非常奇怪的事情:-)

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

会产生错误,就像

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

但奇怪的是,这是有效的:

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

您可以通过以下方式隐藏 fbox 规则:

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

上图由以下材料制成:

\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}

相关内容