如何使用 \resizebox 但不调整文本大小(tikzpicture)

如何使用 \resizebox 但不调整文本大小(tikzpicture)

我从 GeoGebra 导出一张图片并缩放它,例如

\begin{figure}[H]
\centering 

\resizebox{0.5\textwidth}{!}{%
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\draw(0.,0.) circle (1.cm);
\draw (0.,0.)-- (2.,2.);
\draw(3.,3.) circle (2.cm);
\draw [rotate around={0.:(2.,4.)}] (2.,4.) ellipse (2.5cm and 1.5cm);
\draw(-1.,2.) circle (1.cm);
\draw (-2,2.) node[anchor=east] {$e_1$};
\draw (-1,0.) node[anchor=north west] {$e_2$};
\draw (1.,1.) node[anchor=north west] {$e_3$};
\draw (-0.5,4.) node[anchor=east] {$e_4$};
\draw (3.,1.) node[anchor=north] {$e_5$};
\draw (0.,0.) node[anchor=north ] {$v_1$};
\draw (0.,4.) node[anchor=north ] {$v_2$};
\draw (2.,4.) node[anchor=north ] {$v_3$};
\draw (4.,4.) node[anchor=north ] {$v_4$};
\draw (2.,2.) node[anchor=north west ] {$v_5$};
\draw (4.,2.) node[anchor=north] {$v_6$};
\begin{scriptsize}
\draw [fill=black] (0.,0.) circle (1.5pt);
\draw [fill=black] (2.,4.) circle (1.5pt);
\draw [fill=black] (4.,4.) circle (1.5pt);
\draw [fill=black] (2.,2.) circle (1.5pt);
\draw [fill=black] (4.,2.) circle (1.5pt);
\draw [fill=black] (0.,4.) circle (1.5pt);
\end{scriptsize}
\end{tikzpicture}
}
\end{figure}

然后图中的所有东西,包括文本都会被缩放。

但是有一个问题,当我导出另一张与之前尺寸不同的图片并再次使用“resizebox”时,文本的大小与之前图片中的文本大小不相等。

\begin{figure}[H]
\centering
\resizebox{0.5\textwidth}{!}{%
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm][font=\normalsize]
\draw [rotate around={-27.087902355643216:(2.8573157200548693,-0.14756503735849646)}] (2.8573157200548693,-0.14756503735849646) ellipse (2.5481820433499047cm and 1.5397201856917841cm);
\draw [rotate around={34.683048757121114:(4.67999090913984,-0.5899302005094594)}] (4.67999090913984,-0.5899302005094594) ellipse (3.3609235422094357cm and 2.528507164935098cm);
\draw [rotate around={-27.087902355643266:(3.9876766057699897,1.195446721504902)}] (3.9876766057699897,1.195446721504902) ellipse (2.5481820433499047cm and 1.5397201856917841cm);
\draw [rotate around={34.683048757121:(2.040722758983297,1.599931430026935)}] (2.040722758983297,1.599931430026935) ellipse (3.3609235422094303cm and 2.528507164935095cm);
\draw (-0.3,4.12) node[anchor=north west] {$e_2$};
\draw (7.98,-0.32) node[anchor=north west] {$e_4$};
\draw (2.42,3.56) node[anchor=north west] {$e_3$};
\draw (-0.1,0.66) node[anchor=north west] {$e_1$};
\draw (4.30,0.08) node[anchor=north west] {$v_3$};
\draw (2.16,1.54) node[anchor=north west] {$v_1$};
\draw (2.36,-0.16) node[anchor=north west] {$v_2$};
\draw (4.30,1.78) node[anchor=north west] {$v_4$};
\begin{scriptsize}
\draw [fill=black] (2.24,1.3) circle (1.5pt);
\draw [fill=black] (4.30,-0.16) circle (1.5pt);
\draw [fill=black] (4.36,1.54) circle (1.5pt);
\draw [fill=black] (2.36,-0.4) circle (1.5pt);
\end{scriptsize}
\end{tikzpicture}
}
\end{figure}

有没有办法让两个图形中的文字大小相等?

答案1

您应该缩放坐标而不是整个图片。但是缩放 50% 也需要修改某些元素的位置。

以正常尺寸且缩放至 75% 的图形为例:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,arrows}

\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\draw(0.,0.) circle (1.cm);
\draw (0.,0.)-- (2.,2.);
\draw(3.,3.) circle (2.cm);
\draw [rotate around={0.:(2.,4.)}] (2.,4.) ellipse (2.5cm and 1.5cm);
\draw(-1.,2.) circle (1.cm);
\draw (-2,2.) node[anchor=east] {$e_1$};
\draw (-1,0.) node[anchor=north west] {$e_2$};
\draw (1.,1.) node[anchor=north west] {$e_3$};
\draw (-0.5,4.) node[anchor=east] {$e_4$};
\draw (3.,1.) node[anchor=north] {$e_5$};
\draw (0.,0.) node[anchor=north ] {$v_1$};
\draw (0.,4.) node[anchor=north ] {$v_2$};
\draw (2.,4.) node[anchor=north ] {$v_3$};
\draw (4.,4.) node[anchor=north ] {$v_4$};
\draw (2.,2.) node[anchor=north west ] {$v_5$};
\draw (4.,2.) node[anchor=north] {$v_6$};
\draw [fill=black] (0.,0.) circle (1.5pt);
\draw [fill=black] (2.,4.) circle (1.5pt);
\draw [fill=black] (4.,4.) circle (1.5pt);
\draw [fill=black] (2.,2.) circle (1.5pt);
\draw [fill=black] (4.,2.) circle (1.5pt);
\draw [fill=black] (0.,4.) circle (1.5pt);
\end{tikzpicture}

\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm,scale=0.75]
\draw(0.,0.) circle (1.cm);
\draw (0.,0.)-- (2.,2.);
\draw(3.,3.) circle (2.cm);
\draw [rotate around={0.:(2.,4.)}] (2.,4.) ellipse (2.5cm and 1.5cm);
\draw(-1.,2.) circle (1.cm);
\draw (-2,2.) node[anchor=east] {$e_1$};
\draw (-1,0.) node[anchor=north west] {$e_2$};
\draw (1.,1.) node[anchor=north west] {$e_3$};
\draw (-0.5,4.) node[anchor=east] {$e_4$};
\draw (3.,1.) node[anchor=north] {$e_5$};
\draw (0.,0.) node[anchor=north ] {$v_1$};
\draw (0.,4.) node[anchor=north ] {$v_2$};
\draw (2.,4.) node[anchor=north ] {$v_3$};
\draw (4.,4.) node[anchor=north ] {$v_4$};
\draw (2.,2.) node[anchor=north west ] {$v_5$};
\draw (4.,2.) node[anchor=north] {$v_6$};
\draw [fill=black] (0.,0.) circle (1.5pt);
\draw [fill=black] (2.,4.) circle (1.5pt);
\draw [fill=black] (4.,4.) circle (1.5pt);
\draw [fill=black] (2.,2.) circle (1.5pt);
\draw [fill=black] (4.,2.) circle (1.5pt);
\draw [fill=black] (0.,4.) circle (1.5pt);
\end{tikzpicture}

\end{document}

在此处输入图片描述

我删除了无用的(并且错误的)scriptsize声明。

答案2

\resizebox\scalebox、 选项transform canvas={scale=...}都会缩放所有内容,包括线宽和文本。因此,您更需要选项scale,它只缩放坐标。

然而,计算要困难得多,因为绘图由可缩放和不可缩放元素组成。由于后者,绘图通常具有最小尺寸,最小缩放因子也无法减小。此外,节点定位可能需要手动调整,特别是当缩放因子与未缩放情况有很大不同时。

以下示例使用近似法来获取所需宽度。环境imgresize已定义。它首先通过包捕获内容environ。然后使用将捕获的绘图设置为其自然大小scale=1。计算与所需宽度的差值和新的缩放因子。如果宽度差大于阈值,则计算新的缩放因子并测量具有新缩放因子的绘图。重复此操作,直到宽度差不超过阈值。

阈值为 0.1 pt 的示例文件:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{environ}

\makeatletter
\newdimen\imgresize@width
\newdimen\imgresize@threshold
\setlength{\imgresize@threshold}{.1pt}

\NewEnviron{imgresize}[1]{%
  \pgfmathsetlength\imgresize@width{#1}%
  \edef\BODY{% remove spaces at the begin and end
    \ignorespaces\unexpanded\expandafter{\BODY}\unskip
  }%
  \typeout{}%
  %
  \def\imgresize@scale{1}%
  \tikzset{every picture/.style={scale=\imgresize@scale}}%
  \count@=\@ne
  \typeout{* imgresize: try=\the\count@, scale=\imgresize@scale}%
  \sbox0{\BODY}%
  \pgfmathsetlengthmacro\imgresize@diff{abs(\imgresize@width-\wd0)}%
  \@whiledim\imgresize@diff>\imgresize@threshold\do{%
    \let\imgresize@oldscale\imgresize@scale
    \pgfmathsetmacro\imgresize@scale{%
      \imgresize@width*\imgresize@scale/\the\wd0
    }%
    \ifx\imgresize@scale\imgresize@oldscale
      \PackageWarning{imgresize}{%
        Scale factor does not change anymore,\MessageBreak
        width difference is \imgresize@diff,\MessageBreak
        larger than threshold \the\imgresize@threshold
      }%
      \def\imgresize@diff{0pt}% abort loop
    \else
      \advance\count@\@ne
      \typeout{* imgresize: try=\the\count@, scale=\imgresize@scale}%
      \sbox0{\BODY}%
      \pgfmathsetlengthmacro\imgresize@diff{abs(\imgresize@width-\wd0)}%
    \fi
  }%
  \usebox{0}%
}
\makeatother

\begin{document}
\begin{figure}
\centering
\begin{imgresize}{0.49\textwidth}
  \begin{tikzpicture}[
    line cap=round,
    line join=round,
    >=triangle 45,
    x=1.0cm,
    y=1.0cm,
  ]
    \draw(0.,0.) circle (1.cm);
    \draw (0.,0.)-- (2.,2.);
    \draw(3.,3.) circle (2.cm);
    \draw [rotate around={0.:(2.,4.)}] (2.,4.) ellipse (2.5cm and 1.5cm);
    \draw(-1.,2.) circle (1.cm);
    \draw (-2,2.) node[anchor=east] {$e_1$};
    \draw (-1,0.) node[anchor=north west] {$e_2$};
    \draw (1.,1.) node[anchor=north west] {$e_3$};
    \draw (-0.5,4.) node[anchor=east] {$e_4$};
    \draw (3.,1.) node[anchor=north] {$e_5$};
    \draw (0.,0.) node[anchor=north ] {$v_1$};
    \draw (0.,4.) node[anchor=north ] {$v_2$};
    \draw (2.,4.) node[anchor=north ] {$v_3$};
    \draw (4.,4.) node[anchor=north ] {$v_4$};
    \draw (2.,2.) node[anchor=north west ] {$v_5$};
    \draw (4.,2.) node[anchor=north] {$v_6$};
    \draw [fill=black] (0.,0.) circle (1.5pt);
    \draw [fill=black] (2.,4.) circle (1.5pt);
    \draw [fill=black] (4.,4.) circle (1.5pt);
    \draw [fill=black] (2.,2.) circle (1.5pt);
    \draw [fill=black] (4.,2.) circle (1.5pt);
    \draw [fill=black] (0.,4.) circle (1.5pt);
  \end{tikzpicture}
\end{imgresize}\hfill
\begin{imgresize}{.49\textwidth}
  \begin{tikzpicture}[
    line cap=round,
    line join=round,
    >=triangle 45,
    x=1.0cm,
    y=1.0cm,
  ][font=\normalsize]
    \draw [rotate
    around={-27.087902355643216:(2.8573157200548693,-0.14756503735849646)}]
    (2.8573157200548693,-0.14756503735849646) ellipse (2.5481820433499047cm and
    1.5397201856917841cm);
    \draw [rotate
    around={34.683048757121114:(4.67999090913984,-0.5899302005094594)}]
    (4.67999090913984,-0.5899302005094594) ellipse (3.3609235422094357cm and
    2.528507164935098cm);
    \draw [rotate
    around={-27.087902355643266:(3.9876766057699897,1.195446721504902)}]
    (3.9876766057699897,1.195446721504902) ellipse (2.5481820433499047cm and
    1.5397201856917841cm);
    \draw [rotate
    around={34.683048757121:(2.040722758983297,1.599931430026935)}]
    (2.040722758983297,1.599931430026935) ellipse (3.3609235422094303cm and
    2.528507164935095cm);
    \draw (-0.3,4.12) node[anchor=north west] {$e_2$};
    \draw (7.98,-0.32) node[anchor=north west] {$e_4$};
    \draw (2.42,3.56) node[anchor=north west] {$e_3$};
    \draw (-0.1,0.66) node[anchor=north west] {$e_1$};
    \draw (4.30,0.08) node[anchor=north west] {$v_3$};
    \draw (2.16,1.54) node[anchor=north west] {$v_1$};
    \draw (2.36,-0.16) node[anchor=north west] {$v_2$};
    \draw (4.30,1.78) node[anchor=north west] {$v_4$};
    \draw [fill=black] (2.24,1.3) circle (1.5pt);
    \draw [fill=black] (4.30,-0.16) circle (1.5pt);
    \draw [fill=black] (4.36,1.54) circle (1.5pt);
    \draw [fill=black] (2.36,-0.4) circle (1.5pt);
  \end{tikzpicture}
\end{imgresize}
\end{figure}
\end{document}

结果

日志输出:

* imgresize: try=1, scale=1
* imgresize: try=2, scale=0.78491
* imgresize: try=3, scale=0.76906
* imgresize: try=4, scale=0.76756

* imgresize: try=1, scale=1
* imgresize: try=2, scale=0.5902
* imgresize: try=3, scale=0.56789
* imgresize: try=4, scale=0.56583

两次绘图经过四次尝试后的最终缩放因子分别为 0.76756 和 0.56583。第二次绘图的数字相当小,也表明节点位置不再是最佳的。

最小化门槛

\setlength{\imgresize@threshold}{0pt}

结果是:

* imgresize: try=1, scale=1
* imgresize: try=2, scale=0.78491
* imgresize: try=3, scale=0.76906
* imgresize: try=4, scale=0.76756
* imgresize: try=5, scale=0.76743
* imgresize: try=6, scale=0.76741

Package imgresize Warning: Scale factor does not change anymore,
(imgresize)                width difference is 0.00093pt,
(imgresize)                larger than threshold 0.0pt on input line 83.


* imgresize: try=1, scale=1
* imgresize: try=2, scale=0.5902
* imgresize: try=3, scale=0.56789
* imgresize: try=4, scale=0.56583
* imgresize: try=5, scale=0.56563
* imgresize: try=6, scale=0.56561

Package imgresize Warning: Scale factor does not change anymore,
(imgresize)                width difference is 0.0007pt,
(imgresize)                larger than threshold 0.0pt on input line 121.

差异非常小,警告可以忽略。

警告:

由于绘图的最小尺寸,循环可能会永远重复。理论上,可以通过零比例因子来计算最小尺寸。但是,例如,绘图可能会因除以零的错误而出现严重故障。该示例在循环中检查新的比例因子是否已更改,如果比例因子没有更改,但宽度差大于阈值,则会发出警告。

相关内容