使用 matlab2tikz 将 Matlab 图形的外框缩放至恰好 15.5 厘米宽

使用 matlab2tikz 将 Matlab 图形的外框缩放至恰好 15.5 厘米宽

我想将我的 Matlab 图形在 LaTeX 文档中缩放到精确的 15.5 厘米。我正在像这样创建图形:

figure('Name','FigureName','units','centimeters','position',[0 0 15.5 7.75])

我正在使用 matlab2tikz 导出图形:

cleanfigure
matlab2tikz('/Users/.../Figures/Ergebnisse/CentimetersTest.tikz')

我正在像这样嵌入 .tikz 文件:

\begin{figure}[h]
        \centering
         \input{Figures/Ergebnisse/CentimetersTest.tikz}
\caption{CentimetersTest}
\label{CentimetersTest}
\end{figure}

这使得我的图形宽度略小,约为 14 厘米。有人知道如何解决这个问题吗?

先感谢您!

答案1

请尝试以下操作:

  • 在文档的序言中,请确保包含:
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pdflscape}
\pgfplotsset{compat=newest}
%% the following commands are needed for some matlab2tikz features
\usetikzlibrary{plotmarks}
\usetikzlibrary{arrows.meta}
\usepgfplotslibrary{patchplots}
\usepackage{grffile}
\newlength\figurewidth
  • 使用 Matlab 中的命令matlab2tikz('myfigure.tex','width', '\figurewidth' );生成你的 tikzpicture
  • 在 LaTex 中使用
\begin{figure}
    \centering
    \setlength\figurewidth{15.5cm} %or any other LaTex compatible length
    \input{myfigure.tex}
    \caption{A caption}
    \label{fig:label}
\end{figure}

希望这有用

答案2

我刚刚遇到了同样的问题,并在 matlab 中做了一个愚蠢的解决方法。我在绘制数据的实际轴周围创建了一个具有所需大小的空轴。这使得 matlab2tikz 可以根据需要缩放图形。

首先在 Matlab 中:

Height=7.75;
Width=15.5;
AxPos=[2,1,Width-2, Height-1]; % if you subtract a bit more than 2cm in x-direction and 1cm in y direction, the ticks are not cut off.
Fig=figure;
Fig.Units='centimeters';
Fig.Position=[2,4,Width,Height]; 
AxEmpty=gca;
AxEmpty.Units='centimeters';
AxEmpty.YColor=[1 1 1];
AxEmpty.XColor=[1 1 1];
AxEmpty.Position=[0 0 Width Height];
set(AxEmpty,'xtick',[],'ytick',[]);
Ax=axes;
Ax.Units='centimeters';
Ax.Position=AxPos;
% Ax.OuterPosition=[0 0 Width Height]; % Use this line instead of the previous one if you don't want to mess around with spacings around the box.
plot([1 2 3],[1 2 3])

然后用 matlab2tikz 导出:

matlab2tikz('figurehandle',Fig,'filename', 'myfigure.tex','width','\figurewidth');

然后按照 symduk 的回答中的建议,用 Tex 输入它。希望这能有所帮助。

相关内容