问题
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).
For example:
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}
Am I doing something wrong?
回答1:
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 print
ed them as PDFs.
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
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);
来源:https://stackoverflow.com/questions/62040454/matlab-figures-dont-have-the-same-size-when-inserted-in-latex-although-produce