【问题标题】:MATLAB figures don't have the same size when inserted in LaTeX (Although produced using the same code)插入 LaTeX 时,MATLAB 图形的大小不同(尽管使用相同的代码生成)
【发布时间】:2023-03-06 04:16:01
【问题描述】:

我在 MATLAB 中制作了一些图形,并尝试将它们插入到 LaTeX 中。但是,当我这样做时,它们通常没有相同的大小(尽管我使用相同的设置来制作它们)。

例如:

我目前使用的MATLAB代码就是这个

lsize = 16; % Label fontsize
nsize = 16; % Axis fontsize

q=randn(100,1000);

a1=linspace(1,1000,1000);
b1=linspace(2,2000,1000);


figure (1)

histogram(q)

xlabel('Time [sec]','Fontsize', lsize)
ylabel('W_{kin} [keV]','Fontsize', lsize)

set(gca, 'Fontsize', nsize)
set(gcf,'paperpositionmode','auto');
set(gcf,'windowstyle','normal');
set(gca,'LooseInset',max(get(gca,'TightInset'), 0.02))
set(gca,'fontweight','normal')


opts.Colors     = get(groot,'defaultAxesColorOrder');
opts.saveFolder = 'img/';
opts.width      = 12;
opts.height     = 10;
opts.fontType   = 'Times';

saveas(gcf,'f1.png')

figure(2)

loglog(a1,b1)
xlabel('time [sec]','Fontsize', lsize)
ylabel('Speed [m/sec]','Fontsize', lsize)

set(gca, 'Fontsize', nsize)
set(gcf,'paperpositionmode','auto');
set(gcf,'windowstyle','normal');
set(gca,'LooseInset',max(get(gca,'TightInset'), 0.02))
set(gca,'fontweight','normal')


opts.Colors     = get(groot,'defaultAxesColorOrder');
opts.saveFolder = 'img/';
opts.width      = 12;
opts.height     = 10;
opts.fontType   = 'Times';

saveas(gcf,'f2.png')

我使用的乳胶代码是:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}   % needed for figures

\begin{document}

\begin{figure}[!ht]
     \begin{center}

         \includegraphics[width=0.3\textwidth]{f2.png}\\ 

         \includegraphics[width=0.3\textwidth]{f1.png}


     \caption {A caption}\label{A_label}
     \end{center}
     \end{figure}

\end{document}

我是不是做错了什么?

【问题讨论】:

  • 不相关的旁注:get/set 已通过使用句柄被图形引擎大修取代,例如fig = figure()line_handle = plot() 等。减少混乱的代码,你可以这样做 fig.windowstyle = 'normal'。对于子图,您可能还想看看tiledlayout,它或多或少取代了subplot,并且有更多的设计控制选项。
  • 对于 LaTeX 文档,我建议使用矢量图形文件,例如 eps 或 pdf。要对 MATLAB 方面与图形相关的所有内容进行高级控制,我建议您查看 File Exchange 上的export_fig()
  • @Adriaan 我以前看过这个文件,但我无法使用它?出于我的目的,您能否详细说明其中包含的哪些功能有用?
  • 它或多或少是一个工具箱。 export_fig.m 是主要功能。这在它的帮助和 FEX 上有足够的文档。
  • 与您的问题无关,但我建议不要在图中使用center 环境。相反,可以使用\centering。这将避免中心的额外垂直空间

标签: matlab plot latex matlab-figure figure


【解决方案1】:

我在尝试在 LaTeX 中绘制大量数据时遇到了类似的问题(它不是为此而设计的),所以我想在 MATLAB 中绘制图形并在 LaTeX 中排列它们(和轴)。所以我将它们printed 为 PDF。

始终满足确切图形大小的技巧是使坐标区用set(gca,'position',[0 0 1 1]) 填充整个图形。您需要在 LaTeX 中绘制坐标轴、刻度和标签(记得在 pgfplots 中使用选项 axis on top)。

function printFig2PDF(fh,FigName,FigWidth,FigHeight)
%% export MATLAB-figure as PDF

Format = 'pdf';

% check if input name has an extension
lst = strsplit(FigName,'.');
if ~strcmpi(lst{end},Format)
    % append format
    FigName = strcat(FigName,'.',lower(Format));
end

%% adjust figure
if ~isempty(fh.ax.Legend)
    fh.ax.Legend.Visible = 'off';
end
fh.ax.Box = 'off';
set(    fh.ax, 'YTickLabel',{},'XTickLabel',{});
set(    fh.ax, 'yColor','none','xColor','none');


set(fh.ax, 'Position',[0 0 1 1])

set(fh.fig, 'PaperUnits','centimeters',...
        'PaperPosition',[0 0 FigWidth FigHeight],...
        'PaperSize',[FigWidth FigHeight]);

% save as PDF
print(fh.fig,FigName,'-dpdf')
% close figure handle
close(fh.fig)
end

请注意,我假设第一个输入 (fh) 是 struct,其中字段 fig 作为图形句柄,ax 包含轴句柄(这是我存储这些句柄的方式,如果我有多个数字和子图)。如果你想只用一个轴绘制当前图形,你可以用

fh = struct('fig',gcf, 'ax',gca);

【讨论】:

  • 非常感谢您花时间回复!祝你有美好的一天!
  • 很抱歉再次打扰您。我试过你的代码,效果很好。但是,我似乎没有得到轴刻度(我很难在乳胶上绘制)。有没有办法防止这种情况?再次感谢您!
  • 我故意禁用了它们(yColorxColor 所在的行是 set),因为如果 LaTeX 再次将它们绘制在图形的顶部看起来很愚蠢......它应该在pgfplots 中绘制它们并不难。我认为是this 问题
猜你喜欢
  • 2012-10-22
  • 1970-01-01
  • 2011-10-03
  • 1970-01-01
  • 1970-01-01
  • 2014-11-17
  • 2016-10-15
  • 2023-03-24
  • 2014-06-23
相关资源
最近更新 更多