插入LaTeX时,MATLAB图形的大小不相同(尽管使用相同的代码生成) [英] MATLAB figures don't have the same size when inserted in LaTeX (Although produced using the same code)

查看:99
本文介绍了插入LaTeX时,MATLAB图形的大小不相同(尽管使用相同的代码生成)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在MATLAB中生成一些图形,然后尝试将其插入LaTeX.但是,当我这样做时,它们通常没有相同的尺寸(尽管我使用相同的设置来生产它们).

I am producing some figures in MATLAB and try to insert them in LaTeX. However, when I do so, they usually don't have the same size (although I am using the same setup to produce them).

例如:

我当前正在使用的MATLAB代码就是这个

The MATLAB code I am currently using is this one

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')

我正在使用的乳胶代码是:

The latex code I am using is:

\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}

我做错什么了吗?

推荐答案

我试图在LaTeX中绘制海量数据时遇到类似的问题(这并非出于此目的),所以我想在MATLAB中绘制图形并进行排列(和轴)在LaTeX中.因此,我将它们打印为PDF.

I had a similar issue when trying to plot massive data in LaTeX (which it is not made for) so I wanted to draw the figures in MATLAB and arrange them (and the axes) in LaTeX. So I printed them as PDFs.

始终满足精确图形尺寸的技巧是使轴使用 set(gca,'position',[0 0 1 1])填充整个图形.您将需要在LaTeX中绘制轴,刻度和标签(请记住在此处使用 pgfplots 中顶部的 axis 选项).

The trick to always meet the exact figure sizes is to make the axes fill the whole figure with set(gca,'position',[0 0 1 1]). You will need to draw the axes, ticks, and labels in LaTeX (remember to use the option axis on top in pgfplots there).

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 )是具有字段 fig struct (作为图形句柄)和 ax 包含一个轴手柄(如果我有多个图形和子图,这就是我存储这些手柄的方式).如果只想绘制一个轴的当前图形,则可以使用

Note that I assume that the first input (fh) is a struct with the fields fig which as a figure handle and ax which contains an axes handle (which is how I store those handles if I have multiple figures and subplots). If you want to plot the current figure with only one axes, you can create it with

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

这篇关于插入LaTeX时,MATLAB图形的大小不相同(尽管使用相同的代码生成)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆