我想gnuplottex
在子图(来自包)中使用 gnuplot 环境(来自subcaption
包)。
我怎样才能更改 gnuplot 图形的大小,以便它适合子图并且不与旁边的图形重叠?
这是我使用的代码:
\documentclass{scrbook}
\usepackage[utf8]{inputenx}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{epstopdf}
\usepackage{subcaption}
\usepackage[miktex]{gnuplottex}
\begin{document}
\begin{figure}[htbp]
\begin{subfigure}[b]{.5\textwidth}
\begin{gnuplot}[terminal=cairolatex]
plot sin(x)
\end{gnuplot}
\caption{A subfigure}
\end{subfigure}%
\begin{subfigure}[b]{.5\textwidth}
\begin{gnuplot}[terminal=cairolatex]
plot sin(x)
\end{gnuplot}
\caption{Another subfigure}
\end{subfigure}
\caption{A figure}
\end{figure}
\end{document}
其结果如下:
编辑:John 评论了这个解决方案:\begin{gnuplot}[terminal=cairolatex]
用替换\begin{gnuplot}[terminal=cairolatex, terminaloptions={size 7cm, 4cm}]
。现在我有后续问题:是否可以将尺寸指定为0.4\textwidth
而不是7cm
?我可以只指定宽度而不指定高度并保持纵横比吗?
答案1
我在另一个问题上找到了一篇回答我的问题的帖子:如何更改 gnuplot 输出的纵横比?
这就是我解决问题的方法:
\documentclass{scrbook}
\usepackage[utf8]{inputenx}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{epstopdf}
\usepackage{subcaption}
\usepackage{gnuplottex}
\usepackage{xparse}
\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\convertlen}{ O{cm} m }
{
\dim_to_unit:nn { #2 } { 1 #1 } cm
}
\ExplSyntaxOff
\begin{document}
\begin{figure}[htbp]
\centering
\begin{subfigure}[b]{.45\textwidth}
\begin{gnuplot}[terminal = cairolatex, terminaloptions = {size \convertlen{\textwidth},\convertlen{.3\textheight}}]
plot sin(x)
\end{gnuplot}
\caption{A subfigure}
\label{fig:1a}
\end{subfigure}%
\hfill
\begin{subfigure}[b]{.45\textwidth}
\begin{gnuplot}[terminal = cairolatex, terminaloptions = {size \convertlen{\textwidth},\convertlen{.3\textheight}}]
plot sin(x)
\end{gnuplot}
\caption{Another subfigure}
\label{fig:1b}
\end{subfigure}
\caption{A figure}\label{fig:1}
\end{figure}
\end{document}
答案2
以上方法都不适用于我,所以这是我的解决方案,改编自这里:
% first define the conversion command in the preamble
\makeatletter
\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1}
\makeatother
% then use it like this
\begin{gnuplot}[terminal=epslatex, terminaloptions={size \convertto{cm}{\textwidth}cm, 7cm font 8}]
...
我只花了 3 个小时,所以享受吧 :)
答案3
奇怪的是,这似乎不适用于 cairolatex 终端,但对我来说,在 tikz 终端上可以完美运行(如果您还没有使用过 luatex,则可能需要切换到 luatex。不过,从 pdftex 切换到 luatex 很容易。)主要思想是从 Setfan_K 那里偷来的(http://www.latex-community.org/forum/viewtopic.php?f=5&t=2712)
\newlength\textwidthcm
\newlength\widthgnuplot
\newlength\heightgnuplot
\textwidthcm=.03514598035146\textwidth %-- conversion from pt to cm
\widthgnuplot=0.4\textwidthcm %-- conversion to desired width
\heightgnuplot=0.57142857142\widthgnuplot %-- keep aspectratio (prefactor) for height
{\catcode`p=12 \catcode`t=12 \gdef\cm#1pt{#1cm}}
\begin{gnuplot}[terminal=tikz,terminaloptions={size \expandafter\cm\the\widthgnuplot,\expandafter\cm\the\heightgnuplot}]
plot sin(x)
\end{gnuplot}
答案4
我刚刚在我的新库 robust-externalize 中添加了 gnuplot。您可以按如下方式执行此操作:
\documentclass{article}
\usepackage{lipsum} % for dummy text
\usepackage{tikz}
% Needs version 2.3 (as of 2023/12/18, not yet pushed to CTAN, so download .sty here https://github.com/leo-colisson/robust-externalize)
\usepackage{robust-externalize}
\robExtConfigure{enable fallback to manual mode} % If you forgot -shell-escape, it gives instructions on how to manually compile the file
\begin{document}
\lipsum[1]
\noindent\begin{CacheMeCode}{gnuplot, cairolatex terminal/.expanded={size \lenToCm{\textwidth},\lenToCm{.5\textwidth}}}
plot sin(x)
\end{CacheMeCode}
\end{document}