在子图环境中无法正确缩放 tikz

在子图环境中无法正确缩放 tikz

\begin{tikzpicture}[scale = ]我知道如何通过或\resizebox或缩放单个 tikz 图片\scalebox。但是,一旦将它们放入子图环境中,这似乎就崩溃了。这就是 tikz 图片单独的样子:

在此处输入图片描述

但它在子图 (第一个面板) 中的样子如下:

在此处输入图片描述

首先,整个图片太小了。但是,一旦我用 将其放大\begin{tikzpicture}[scale=2, every node/.style={scale=0.5}],就会出现面板 (b),其中线条太粗,虚线间距太大。我想要的只是独立图形的缩放版本——我该怎么做?

以下是我的 WE(不是那么简单):

\begin{figure}
  \centering
  \begin{subfigure}[b]{0.49\textwidth}
  \centering
  \resizebox{\linewidth}{!}{%
  \begin{tikzpicture}
    \pgfplotsset{compat=1.17}
    \begin{axis}[
      xlabel={$\Pr(\theta=1)$},
      ylabel={$U$},
      xmin=-0.1, xmax=1,
      axis lines=middle,
      samples=100,
      width=0.6\textwidth,
      xtick=\empty,
      ytick=\empty,
      xlabel style={anchor=west},
      ylabel style={anchor=east},
      clip=false,
      ]
      \addplot[black, thick, domain=0.75:1] {x};
      \addplot[black, dashed, domain=0:0.75] {x};
      \addplot[black, thick, domain=0:0.75] {1 - x};
      \addplot[black, dashed, domain=0.75:1] {1 - x};

      
      % Intersection points
      \coordinate (A) at (0.5, 0.5);
      \coordinate (B) at (0.75, 0.75);
      \coordinate (C) at (0.5, 0);
      \coordinate (D) at (0.75, 0);
      \coordinate (D1) at (0.75, 0);
      \coordinate (E1) at (0.65, 0);
      \coordinate (F1) at (0.85, 0);
      \coordinate (E2) at (0.65,0.35);
      \coordinate (F2) at (0.85,0.85);
      \coordinate (MEAN2) at (0.8,0.8);
      \coordinate (MEAN1) at (0.8,0.74);

      % Point indicators
      \node[draw,circle,inner sep=1pt,fill=black] at (C) {};
      \node[draw,circle,inner sep=1pt,fill=black] at (D) {};
      \node[draw,circle,inner sep=1pt,fill=none] at (E1) {};
      \node[draw,circle,inner sep=1pt,fill=none] at (F1) {};
      \node[draw,circle,inner sep=1.5pt,fill=red] at (E2) {};
      \node[draw,circle,inner sep=1.5pt,fill=red] at (F2) {};
      \draw[thick, red, dashed] (E2) -- (F2);
      \node[draw,diamond,inner sep=1.5pt,fill=red] at (MEAN1) {};
      \node[draw,diamond,inner sep=1.5pt,fill=black] at (MEAN2) {};

      % Dashed line and label
      \node[below left] at (0, 0) {$0$};
      \node[below] at (1, 0) {1};
      \draw[dashed] (A) -- (0.5, 0);
      \node[below right] at (0.5, 0) {$\bar{\mu}_P$};
      \draw[dashed] (B) -- (0.75, 0);
      \node[below right] at (0.75, 0) {$\bar{\mu}_A$};
      \node[below right] at (E1) {$\mu_{10}$};
      \node[below right] at (F1) {$\mu_{11}$};
      \draw[dashed] (MEAN2) -- (0.8,0);

    \end{axis}
  \end{tikzpicture}
  }
  \caption[]{{\small Ex1}}    
  \end{subfigure}%
  \begin{subfigure}[b]{0.49\textwidth}
  \centering 
  \resizebox{\linewidth}{!}{%
  \begin{tikzpicture}[scale=2, every node/.style={scale=0.5}]
    \pgfplotsset{compat=1.17}
    \begin{axis}[
      xlabel={$\Pr(\theta=1)$},
      ylabel={$U$},
      xmin=-0.1, xmax=1,
      axis lines=middle,
      samples=100,
      width=0.6\textwidth,
      xtick=\empty,
      ytick=\empty,
      xlabel style={anchor=west},
      ylabel style={anchor=east},
      clip=false,
      ]
      \addplot[black, thick, domain=0.75:1] {x};
      \addplot[black, dashed, domain=0:0.75] {x};
      \addplot[black, thick, domain=0:0.75] {1 - x};
      \addplot[black, dashed, domain=0.75:1] {1 - x};

      
      % Intersection points
      \coordinate (A) at (0.5, 0.5);
      \coordinate (B) at (0.75, 0.75);
      \coordinate (C) at (0.5, 0);
      \coordinate (D) at (0.75, 0);
      \coordinate (D1) at (0.75, 0);
      \coordinate (E1) at (0.7, 0);
      \coordinate (F1) at (0.45, 0);
      \coordinate (E2) at (0.7,0.3);
      \coordinate (F2) at (0.45,0.55);
      \coordinate (MEAN1) at (0.6,0.4);
      \coordinate (MEAN2) at (0.6,0.6);

      % Point indicators
      \node[draw,circle,inner sep=1pt,fill=black] at (C) {};
      \node[draw,circle,inner sep=1pt,fill=black] at (D) {};
      \node[draw,circle,inner sep=1pt,fill=none] at (E1) {};
      \node[draw,circle,inner sep=1pt,fill=none] at (F1) {};
      \node[draw,circle,inner sep=1.5pt,fill=red] at (E2) {};
      \node[draw,circle,inner sep=1.5pt,fill=red] at (F2) {};
      \draw[thick, red, dashed] (E2) -- (F2);
      \node[draw,diamond,inner sep=1.5pt,fill=red] at (MEAN1) {};
      \node[draw,diamond,inner sep=1.5pt,fill=black] at (MEAN2) {};

      % Dashed line and label
      \node[below left] at (0, 0) {$0$};
      \node[below] at (1, 0) {1};
      \draw[dashed] (A) -- (0.5, 0);
      %\node[below right] at (0.5, 0) {$\bar{\mu}_P$};
      \draw[dashed] (B) -- (0.75, 0);
      %\node[below right] at (0.75, 0) {$\bar{\mu}_A$};
      \node[below] at (E1) {$\mu_{11}$};
      \node[below] at (F1) {$\mu_{10}$};
      \draw[dashed] (MEAN2) -- (0.6, 0);
      \node[below] at (0.6, 0) {$\mu_1$};

    \end{axis}
  \end{tikzpicture}
  }
  \caption[]{{\small Ex2}}    
  \end{subfigure}
\end{figure}

答案1

这是另一种方法,使用subfiles。此包允许您:

  • preamble使用主文档编译子文件
  • 编译整个主文档
  • 将一些缩放操作移到子文件中。

优势对于工作流程:

  • 与序言一致(只有一个)
  • 单独编译子文件,例如在开发期间
  • 在需要时将所有内容编译在一起
  • 即检查fig1.pdffig2.pdf首先,最后生成main.pdf

让我们看看它是如何工作的。

主文本

非常简单,不是吗?只需在此处提供子文件所需的所有包。例如,一个文件可能只需要Tikz,另一个文件只需要graphicx等等……所有这些都会出现在这里。

主要的

\documentclass[10pt,a4paper]{article}%  probably your preferred class
\usepackage{subfiles}%                  allows joint and individual compiles
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\usepackage{pgfplots}
\usepackage{subfigure}%                 your troublemaker

\begin{document}

 \begin{figure}
    \centering
    \subfigure[Ex1]{\subfile{fig1}}%    <<< new
    \subfigure[Ex2]{\subfile{fig2}}

    \caption{Whatever you want }
 \end{figure}

\end{document}

图1.tex

与通过单独的 PDF 编译包含图像的方法相比,存在重要区别,往上看

  • 现在编译为类article
  • 需要[scale=0.65, transform shape]% <<< new,请参阅 pgfmanual
  • 在这里对宽度进行微调
  • 选项width也许也能起作用,以某种方式......

图。1

\documentclass[main]{subfiles}% = reuse preamble found in main.tex

\begin{document}
  \begin{tikzpicture}[scale=0.65, transform shape]% <<< new
    \pgfplotsset{compat=1.17}
    \begin{axis}[
      xlabel={$\Pr(\theta=1)$},
      ylabel={$U$},
      xmin=-0.1, xmax=1,
      axis lines=middle,
      samples=100,
%      width=0.6\textwidth,
      xtick=\empty,
      ytick=\empty,
      xlabel style={anchor=west},
      ylabel style={anchor=east},
      clip=false,
      ]
      \addplot[black, thick, domain=0.75:1] {x};
      \addplot[black, dashed, domain=0:0.75] {x};
      \addplot[black, thick, domain=0:0.75] {1 - x};
      \addplot[black, dashed, domain=0.75:1] {1 - x};
      
      % Intersection points
      \coordinate (A)       at (0.5 , 0.5);
      \coordinate (B)       at (0.75, 0.75);
      \coordinate (C)       at (0.5 , 0);
      \coordinate (D)       at (0.75, 0);
      \coordinate (D1)      at (0.75, 0);
      \coordinate (E1)      at (0.65, 0);
      \coordinate (F1)      at (0.85, 0);
      \coordinate (E2)      at (0.65, 0.35);
      \coordinate (F2)      at (0.85, 0.85);
      \coordinate (MEAN2)   at (0.8 , 0.8);
      \coordinate (MEAN1)   at (0.8 , 0.74);

      % Point indicators
      \node[draw,circle,inner sep=1pt,fill=black] at (C) {};
      \node[draw,circle,inner sep=1pt,fill=black] at (D) {};
      \node[draw,circle,inner sep=1pt,fill=none] at (E1) {};
      \node[draw,circle,inner sep=1pt,fill=none] at (F1) {};
      \node[draw,circle,inner sep=1.5pt,fill=red] at (E2) {};
      \node[draw,circle,inner sep=1.5pt,fill=red] at (F2) {};
      \draw[thick, red, dashed] (E2) -- (F2);
      \node[draw,diamond,inner sep=1.5pt,fill=red] at (MEAN1) {};
      \node[draw,diamond,inner sep=1.5pt,fill=black] at (MEAN2) {};

      % Dashed line and label
      \node[below left] at (0, 0) {$0$};
      \node[below] at (1, 0) {1};
      \draw[dashed] (A) -- (0.5, 0);
      \node[below right] at (0.5, 0) {$\bar{\mu}_P$};
      \draw[dashed] (B) -- (0.75, 0);
      \node[below right] at (0.75, 0) {$\bar{\mu}_A$};
      \node[below right] at (E1) {$\mu_{10}$};
      \node[below right] at (F1) {$\mu_{11}$};
      \draw[dashed] (MEAN2) -- (0.8,0);
      
    \end{axis}
  \end{tikzpicture}
\end{document}

图2.tex

与 fig1.tex 相同。

图2

\documentclass[main]{subfiles}% = reuse preamble found in main.tex

\begin{document}
  \begin{tikzpicture}[transform shape,scale=0.65]% <<< new
    \pgfplotsset{compat=1.17}
    \begin{axis}[
      xlabel={$\Pr(\theta=1)$},
      ylabel={$U$},
      xmin=-0.1, xmax=1,
      axis lines=middle,
      samples=100,
%      width=0.6\textwidth,
      xtick=\empty,
      ytick=\empty,
      xlabel style={anchor=west},
      ylabel style={anchor=east},
      clip=false,
      ]
      \addplot[black, thick, domain=0.75:1] {x};
      \addplot[black, dashed, domain=0:0.75] {x};
      \addplot[black, thick, domain=0:0.75] {1 - x};
      \addplot[black, dashed, domain=0.75:1] {1 - x};

      
      % Intersection points
      \coordinate (A) at (0.5, 0.5);
      \coordinate (B) at (0.75, 0.75);
      \coordinate (C) at (0.5, 0);
      \coordinate (D) at (0.75, 0);
      \coordinate (D1) at (0.75, 0);
      \coordinate (E1) at (0.7, 0);
      \coordinate (F1) at (0.45, 0);
      \coordinate (E2) at (0.7,0.3);
      \coordinate (F2) at (0.45,0.55);
      \coordinate (MEAN1) at (0.6,0.4);
      \coordinate (MEAN2) at (0.6,0.6);

      % Point indicators
      \node[draw,circle,inner sep=1pt,fill=black] at (C) {};
      \node[draw,circle,inner sep=1pt,fill=black] at (D) {};
      \node[draw,circle,inner sep=1pt,fill=none] at (E1) {};
      \node[draw,circle,inner sep=1pt,fill=none] at (F1) {};
      \node[draw,circle,inner sep=1.5pt,fill=red] at (E2) {};
      \node[draw,circle,inner sep=1.5pt,fill=red] at (F2) {};
      \draw[thick, red, dashed] (E2) -- (F2);
      \node[draw,diamond,inner sep=1.5pt,fill=red] at (MEAN1) {};
      \node[draw,diamond,inner sep=1.5pt,fill=black] at (MEAN2) {};

      % Dashed line and label
      \node[below left] at (0, 0) {$0$};
      \node[below] at (1, 0) {1};
      \draw[dashed] (A) -- (0.5, 0);
      %\node[below right] at (0.5, 0) {$\bar{\mu}_P$};
      \draw[dashed] (B) -- (0.75, 0);
      %\node[below right] at (0.75, 0) {$\bar{\mu}_A$};
      \node[below] at (E1) {$\mu_{11}$};
      \node[below] at (F1) {$\mu_{10}$};
      \draw[dashed] (MEAN2) -- (0.6, 0);
      \node[below] at (0.6, 0) {$\mu_1$};

    \end{axis}
  \end{tikzpicture}
\end{document}

答案2

我建议使用不同的方法:

  • Tikz使用/创建两个单独的文件pgfplotsfig1.tex然后fig2.tex
  • 将它们编译为pdf
  • 将它们包括graphicx在内main.tex

主文本

代码显示了两种方法includegraphics

主要的

\documentclass[10pt,a4paper]{article}
\usepackage{graphicx}
\usepackage{subfigure}

\begin{document}

\begin{figure}
    \subfigure[Ex1]{\includegraphics[height=5cm]{fig1}}
    \subfigure[Ex2]{\includegraphics[height=5cm]{fig2}}
    \caption{Whatever you want \dots one way to do it}
\end{figure}

\begin{figure}
    \subfigure[Ex1]{\includegraphics[width=.45\textwidth]{fig1}}
    \subfigure[Ex2]{\includegraphics[width=.45\textwidth]{fig2}}

    \caption{Whatever you want \dots an other way to do it}
\end{figure}

\end{document}

图1.tex

无宽度规范。类别standalone

图。1

\documentclass[10pt,border=3mm,tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\usepackage{pgfplots}

\begin{document}
  \begin{tikzpicture}
    \pgfplotsset{compat=1.17}
    \begin{axis}[
      xlabel={$\Pr(\theta=1)$},
      ylabel={$U$},
      xmin=-0.1, xmax=1,
      axis lines=middle,
      samples=100,
%      width=0.6\textwidth,
      xtick=\empty,
      ytick=\empty,
      xlabel style={anchor=west},
      ylabel style={anchor=east},
      clip=false,
      ]
      \addplot[black, thick, domain=0.75:1] {x};
      \addplot[black, dashed, domain=0:0.75] {x};
      \addplot[black, thick, domain=0:0.75] {1 - x};
      \addplot[black, dashed, domain=0.75:1] {1 - x};
      
      % Intersection points
      \coordinate (A)       at (0.5 , 0.5);
      \coordinate (B)       at (0.75, 0.75);
      \coordinate (C)       at (0.5 , 0);
      \coordinate (D)       at (0.75, 0);
      \coordinate (D1)      at (0.75, 0);
      \coordinate (E1)      at (0.65, 0);
      \coordinate (F1)      at (0.85, 0);
      \coordinate (E2)      at (0.65, 0.35);
      \coordinate (F2)      at (0.85, 0.85);
      \coordinate (MEAN2)   at (0.8 , 0.8);
      \coordinate (MEAN1)   at (0.8 , 0.74);

      % Point indicators
      \node[draw,circle,inner sep=1pt,fill=black] at (C) {};
      \node[draw,circle,inner sep=1pt,fill=black] at (D) {};
      \node[draw,circle,inner sep=1pt,fill=none] at (E1) {};
      \node[draw,circle,inner sep=1pt,fill=none] at (F1) {};
      \node[draw,circle,inner sep=1.5pt,fill=red] at (E2) {};
      \node[draw,circle,inner sep=1.5pt,fill=red] at (F2) {};
      \draw[thick, red, dashed] (E2) -- (F2);
      \node[draw,diamond,inner sep=1.5pt,fill=red] at (MEAN1) {};
      \node[draw,diamond,inner sep=1.5pt,fill=black] at (MEAN2) {};

      % Dashed line and label
      \node[below left] at (0, 0) {$0$};
      \node[below] at (1, 0) {1};
      \draw[dashed] (A) -- (0.5, 0);
      \node[below right] at (0.5, 0) {$\bar{\mu}_P$};
      \draw[dashed] (B) -- (0.75, 0);
      \node[below right] at (0.75, 0) {$\bar{\mu}_A$};
      \node[below right] at (E1) {$\mu_{10}$};
      \node[below right] at (F1) {$\mu_{11}$};
      \draw[dashed] (MEAN2) -- (0.8,0);
      
    \end{axis}
  \end{tikzpicture}
\end{document}

图2.tex

无宽度规范。类别standalone

图2

\documentclass[10pt,border=3mm,tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\usepackage{pgfplots}

\begin{document}
  \begin{tikzpicture}%[scale=2, every node/.style={scale=0.5}]
    \pgfplotsset{compat=1.17}
    \begin{axis}[
      xlabel={$\Pr(\theta=1)$},
      ylabel={$U$},
      xmin=-0.1, xmax=1,
      axis lines=middle,
      samples=100,
%      width=0.6\textwidth,
      xtick=\empty,
      ytick=\empty,
      xlabel style={anchor=west},
      ylabel style={anchor=east},
      clip=false,
      ]
      \addplot[black, thick, domain=0.75:1] {x};
      \addplot[black, dashed, domain=0:0.75] {x};
      \addplot[black, thick, domain=0:0.75] {1 - x};
      \addplot[black, dashed, domain=0.75:1] {1 - x};

      
      % Intersection points
      \coordinate (A) at (0.5, 0.5);
      \coordinate (B) at (0.75, 0.75);
      \coordinate (C) at (0.5, 0);
      \coordinate (D) at (0.75, 0);
      \coordinate (D1) at (0.75, 0);
      \coordinate (E1) at (0.7, 0);
      \coordinate (F1) at (0.45, 0);
      \coordinate (E2) at (0.7,0.3);
      \coordinate (F2) at (0.45,0.55);
      \coordinate (MEAN1) at (0.6,0.4);
      \coordinate (MEAN2) at (0.6,0.6);

      % Point indicators
      \node[draw,circle,inner sep=1pt,fill=black] at (C) {};
      \node[draw,circle,inner sep=1pt,fill=black] at (D) {};
      \node[draw,circle,inner sep=1pt,fill=none] at (E1) {};
      \node[draw,circle,inner sep=1pt,fill=none] at (F1) {};
      \node[draw,circle,inner sep=1.5pt,fill=red] at (E2) {};
      \node[draw,circle,inner sep=1.5pt,fill=red] at (F2) {};
      \draw[thick, red, dashed] (E2) -- (F2);
      \node[draw,diamond,inner sep=1.5pt,fill=red] at (MEAN1) {};
      \node[draw,diamond,inner sep=1.5pt,fill=black] at (MEAN2) {};

      % Dashed line and label
      \node[below left] at (0, 0) {$0$};
      \node[below] at (1, 0) {1};
      \draw[dashed] (A) -- (0.5, 0);
      %\node[below right] at (0.5, 0) {$\bar{\mu}_P$};
      \draw[dashed] (B) -- (0.75, 0);
      %\node[below right] at (0.75, 0) {$\bar{\mu}_A$};
      \node[below] at (E1) {$\mu_{11}$};
      \node[below] at (F1) {$\mu_{10}$};
      \draw[dashed] (MEAN2) -- (0.6, 0);
      \node[below] at (0.6, 0) {$\mu_1$};

    \end{axis}
  \end{tikzpicture}
\end{document}

相关内容