我在将 pgfplot 的宽度设置为页面宽度时遇到了麻烦。我发现这个帖子其中详细讨论了该问题,但提供的答案不适用external
,其他解决方案会缩放字体。我试图将一个由两个图组成的 pgfplot 插入到我的文档中。这是代码
\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}[]
\begin{groupplot}[group style={group name=myplot, group size= 2 by 1, horizontal sep=2.5em}, width=1\textwidth, every axis plot/.append style={line width=0.4mm, line join=round},
xmin=-1,xmax=1,
ymin=-1,ymax=1.,
]
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x};
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x^2};
\end{groupplot}
\end{tikzpicture}
\end{document}
我已经使用该layouts
包来确定linewidth
和textwidth
参数:
linewidth: \printinunitsof{cm}\prntlen{\linewidth}
textwidth: \printinunitsof{cm}\prntlen{\textwidth}
并且它们都是11.1484cm
。问题是当我使用时,图像超出了这些尺寸width=1\textwidth
。我可以手动调整图像以占据我想要的宽度,但我认为一定有更好的方法。看似如此简单的事情不可能如此难以实现。提供的解决方案应该是通用的,即应该适用于 groupplot 中任意数量的图。字体不应缩放,而应与文档的其余部分保持相同的大小。它的适用性也很重要,\usetikzlibrary{external}
因为我要处理很多图形。
答案1
这只是猜测。但是,它实现了某些功能harmonicnumber
,因此可能并非完全无用。在手头的例子中,它也能正常工作。但必须“猜测”一些参数,而且很可能它通常不起作用,但通过调整,guessed width
您可能会得到可接受的结果。更简洁的方法可能是了解pgfplots
内部发生了什么,或者遵循John Kormylo 的方法。
但我得到的就是这样。calc
图书馆和线条
\path let \p1=($(current bounding box.east)-(current bounding box.west)$)
in \pgfextra{\typeout{width used=\x1, width available=\the\textwidth}};
只是为了检查空间的利用效率。
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.17}
\makeatletter
\pgfmathdeclarefunction*{harmonicnumber}{2}{%
\begingroup%
\pgfmathtruncatemacro\pgfutil@tmpa{#1}%
\ifnum\pgfutil@tmpa>1\relax
\pgfmathsetmacro\pgfutil@tmpb{harmonicnumber(\pgfutil@tmpa-1,#2)}%
\pgfmathparse{\pgfutil@tmpb+1/pow(\pgfutil@tmpa,#2)}%
\else
\pgfmathparse{1}%
\fi
\pgfmathsmuggle\pgfmathresult\endgroup%
}
\pgfplotsset{guess width/.code={%
\pgfmathsetmacro\pgfutil@tmpw{harmonicnumber(\pgfplots@group@columns-1,3)*\pgfkeysvalueof{/pgfplots/guessed offset}+\textwidth/\pgfplots@group@columns}%
\pgfplotsset{width=\pgfutil@tmpw pt}%
},guessed offset/.initial=15pt}
\makeatother
\begin{document}
\noindent
\begin{tikzpicture}[]
\begin{groupplot}[group style={group name=myplot, group size= 2 by 1,
horizontal sep=2.5em},guess width,
every axis plot/.append style={line width=0.4mm, line join=round},
xmin=-1,xmax=1,
ymin=-1,ymax=1.,
]
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x};
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x^2};
\end{groupplot}
\path let \p1=($(current bounding box.east)-(current bounding box.west)$)
in \pgfextra{\typeout{width used=\x1, width available=\the\textwidth}};
\end{tikzpicture}
\noindent
\begin{tikzpicture}[]
\begin{groupplot}[group style={group name=myplot, group size= 3 by 1,
horizontal sep=2.5em},guess width,
every axis plot/.append style={line width=0.4mm, line join=round},
xmin=-1,xmax=1,
ymin=-1,ymax=1.,
]
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x};
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x^2};
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x^3};
\end{groupplot}
\path let \p1=($(current bounding box.east)-(current bounding box.west)$)
in \pgfextra{\typeout{width used=\x1, width available=\the\textwidth}};
\end{tikzpicture}
\noindent
\begin{tikzpicture}[]
\begin{groupplot}[group style={group name=myplot, group size= 4 by 1,
horizontal sep=2.5em},guess width,
every axis plot/.append style={line width=0.4mm, line join=round},
xmin=-1,xmax=1,
ymin=-1,ymax=1.,
]
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x};
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x^2};
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x^3};
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x^4};
\end{groupplot}
\path let \p1=($(current bounding box.east)-(current bounding box.west)$)
in \pgfextra{\typeout{width used=\x1, width available=\the\textwidth}};
\end{tikzpicture}
\end{document}
答案2
该解决方案运行两次绘图,使用第一次运行的实际宽度来调整第二次运行的宽度。
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepackage{showframe}
\newsavebox{\tempbox}
\begin{document}
\savebox{\tempbox}{\begin{tikzpicture}
\begin{groupplot}[group style={group name=myplot, group size= 2 by 1, horizontal sep=2.5em},
width=0.5\textwidth,
every axis plot/.append style={line width=0.4mm, line join=round},
xmin=-1,xmax=1,
ymin=-1,ymax=1.,
]
\nextgroupplot[]
%\addplot + [mark=none, domain=-1:1] {x};
\nextgroupplot[]
%\addplot + [mark=none, domain=-1:1] {x^2};
\end{groupplot}
\end{tikzpicture}}% measure width
\noindent\begin{tikzpicture}[]
\begin{groupplot}[group style={group name=myplot, group size= 2 by 1, horizontal sep=2.5em},
width={\dimexpr \textwidth-0.5\wd\tempbox},
every axis plot/.append style={line width=0.4mm, line join=round},
xmin=-1,xmax=1,
ymin=-1,ymax=1.,
]
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x};
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x^2};
\end{groupplot}
\draw[red] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}
\end{document}
这说明了如何使用 groupwidth 环境仅输入一次代码。
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepackage{environ}
\usepackage{showframe}
\NewEnviron{groupwidth}[1]{% #1 = number of groupplots per row
\tikzset{/pgfplots/width={\dimexpr \textwidth/#1}}%
\sbox0{\BODY}%
\tikzset{/pgfplots/width={\dimexpr 2\textwidth/#1 - \wd0/#1}}%
\BODY}
\begin{document}
\noindent\begin{groupwidth}{2}
\begin{tikzpicture}
\begin{groupplot}[group style={group name=myplot, group size= 2 by 1, horizontal sep=2.5em},
every axis plot/.append style={line width=0.4mm, line join=round},
xmin=-1,xmax=1,
ymin=-1,ymax=1.,
]
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x};
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x^2};
\end{groupplot}
\end{tikzpicture}%
\end{groupwidth}
\end{document}
此版本使用辅助文件在运行之间进行通信。您无需绘制 tikzpicture 两次,而是至少运行整个文档两次。
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepackage{showframe}
\newcommand{\newgroupwidth}[2]% #1 = id, #2 = width to use
{\expandafter\xdef\csname groupwidth#1\endcsname{#2}}
\newcounter{groupwidth}
\newsavebox{\groupwidthbox}
\makeatletter
\newenvironment{groupwidth}[1]% #1 = number of groupplots per row
{\edef\groupnumber{#1}% save for \endgroupwidth
\stepcounter{groupwidth}%
\@ifundefined{groupwidth\thegroupwidth}{\pgfmathsetlengthmacro{\mywidth}{\textwidth/\groupnumber}}%
{\expandafter\let\expandafter\mywidth\csname groupwidth\thegroupwidth\endcsname}%
\begin{lrbox}{\groupwidthbox}%
\tikzset{/pgfplots/width={\mywidth}}%
\ignorespaces}%
{\end{lrbox}%
\usebox\groupwidthbox
\pgfmathsetlengthmacro{\mywidth}{\mywidth + (\textwidth - \wd\groupwidthbox)/\groupnumber}
\immediate\write\@auxout{\string\newgroupwidth{\thegroupwidth}{\mywidth}}}
\makeatother
\begin{document}
\noindent\begin{groupwidth}{2}
\begin{tikzpicture}
\begin{groupplot}[group style={group name=myplot, group size= 2 by 1, horizontal sep=2.5em},
every axis plot/.append style={line width=0.4mm, line join=round},
xmin=-1,xmax=1,
ymin=-1,ymax=1.,
]
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x};
\nextgroupplot[]
\addplot + [mark=none, domain=-1:1] {x^2};
\end{groupplot}
%\draw[red] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}%
\end{groupwidth}
\end{document}
答案3
考虑一下:
创建一个名为的文件groupPlot.tikz
,其中包含以下内容:
% groupPlot.tikz
\begin{tikzpicture}[]
\usepgfplotslibrary{groupplots}
\begin{groupplot}[group style={group name=myplot, group size= 2 by 1, horizontal sep=2.5em},
width=1\textwidth,
every axis plot/.append style={line width=0.4mm, line join=round},
xmin=-1,xmax=1,
ymin=-1,ymax=1.,
]
\nextgroupplot[width=0.5\textwidth]
\addplot + [mark=none, domain=-1:1] {x};
\nextgroupplot[width=0.5\textwidth]
\addplot + [mark=none, domain=-1:1] {x^2};
\end{groupplot}
\end{tikzpicture}
呼吁tikzscale
在您的主文档中打包,然后使用以下includegraphics
命令,如本例所示:
% main.tex
\documentclass{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=newest}
\usepackage{tikzscale}
\usepgfplotslibrary{groupplots}
\usepackage[showframe]{geometry} % For visibility
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{figure}
\centering
\includegraphics[width=\textwidth,height=0.3\textwidth]{groupPlot.tikz}
\caption{Caption}
\label{fig:my_label}
\end{figure}
\end{document}
结果:
作为另一项试验,与book
documentclass 一起使用,修改为tufte-book
布局:
% main.tex
\documentclass{book}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=newest}
\usepackage{tikzscale}
\usepgfplotslibrary{groupplots}
\usepackage[a4paper,left=24.8mm,top=27.4mm,headsep=2\baselineskip,textwidth=107mm,marginparsep=8.2mm,marginparwidth=49.4mm,textheight=49\baselineskip,headheight=\baselineskip,asymmetric,showframe]{geometry} % Tufte book layout
\usepackage{lipsum}
\begin{document}
\chapter*{First Chapter}
\lipsum[1]
\begin{figure}
\centering
\includegraphics[width=\textwidth,height=0.3\textwidth]{groupPlot.tikz}
\caption{Caption}
\label{fig:my_label}
\end{figure}
\end{document}
我收到的唯一信息是
包 tikzscale 警告:groupPlot.tikz 的宽度缩放在输入第 16 行仅精确到 31.749pt。
使用 tikzscale,您可以明确定义宽度与高度的纵横比。因此,您可以将选项更改height=0.3\textwidth
为最适合您的选项。
我只\nextgroupplot[width=0.5\textwidth]
在你的情节中添加了一个,读完之后这个答案。