Ghostscript ps2pdf在MATLAB中无法正常工作



我有一个.tex文件,我正试图从MATLAB环境(2017b(中编写,首先使用latex,然后使用dvips,最后使用ps2pdf来创建PDF文件。当我通过TeXWorks运行PDF文件时,它是正确创建的,但当我尝试运行它时,它会从ps2pdf发送一个错误。我不完全确定我把它发送到命令行的方式出了什么问题。这是我试图运行的脚本。

set(groot, 'defaultAxesTickLabelInterpreter','latex'); 
set(groot, 'defaultLegendInterpreter','latex');
set(0,'defaultTextInterpreter','latex');
addpath('C:Program Files (x86)MiKTeXmiktexbinx64')
exampledir='\ourserver';
examplefilename='test';
texdoc=fullfile(exampledir,strcat(examplefilename,'.tex'));
auxdoc=fullfile(exampledir,strcat(examplefilename,'.aux'));
logdoc=fullfile(exampledir,strcat(examplefilename,'.log'));
dvidoc=fullfile(exampledir,strcat(examplefilename,'.dvi'));
psdoc=fullfile(exampledir,strcat(examplefilename,'.ps'));
pdfdoc=fullfile(exampledir,strcat(examplefilename,'.pdf'));

fileID = fopen(texdoc,'w');
textext=['documentclass[twoside]{article} %Two-sided document. Required for fancyhf left and right page numbering scheme current.' newline ...
'usepackage{graphicx}' newline ...
newline ...
'usepackage{fancyhdr} %Use the package fancy header/footer' newline ...
newline ...
'usepackage[letterpaper,margin=0.5in,bottom=0.75in,top=0.7in]{geometry} %Ensure the paper is letterpaper.' newline ...
'usepackage{grffile}' newline ...
'usepackage{caption}' newline ...
'usepackage{float} %Float used to position graphics.' newline ...
'usepackage{lastpage209} %For last page' newline ...
newline ...
'DeclareGraphicsExtensions{.PDF,.jpg} %Notify LaTeX what type of graphics extensions to expect.' newline ...
newline ...
'renewcommand{headrulewidth}{0pt} % remove the header rule' newline ...
newline ...
'fancyhf{} % clear all header and footers' newline ...
newline ...
'pagestyle{fancy} %Use the fancy pagestyle, which will include the fancy header and footer options we defined above.' newline ...
newline ...
'setlengthheadheight{38pt} ' newline ...
newline ...
'fancyhead[L]{{Page thepage}  of pageref{LastPage}} %Left side on even pages; right side on odd pages.' newline ...
'fancyhead[R]{includegraphics[trim={0.3in 0.26in 0.05in 0.26in},clip,width=0.4in]{{\ourservermypicture}}}' newline ...
newline ...
'begin{document} %This will be the actual document and what goes into it.' newline ...
newline ...
'begin{figure}[h] %Make a figure...' newline ...
newline ...
'       includegraphics[trim={0.25in 0 0 0},clip,width=7.5in]{{\ourservermypicture}}' newline ...
newline ...
'   captionsetup{labelformat=empty}' newline ...
newline ...
'end{figure}' newline ...
newline ...
'end{document}'];
fprintf(fileID,'%s',textext);
fclose(fileID);
%           latex               dvips            ps2pdf
%   text.tex -------> text.dvi -------> text.ps --------> text.pdf
[~,cmdoutlatex] = system(['latex.exe -interaction=nonstopmode -output-directory ' exampledir ' ' texdoc]);
if contains(cmdoutlatex,'Sorry, but latex.exe did not succeed.')
%Do some kind of check and fix things.
end
[~,cmdoutdvips] = system(['dvips.exe -q* -o ' psdoc ' ' dvidoc]);
if contains(cmdoutdvips,'!')
%Do some kind of check and fix things.
error(['Error: ' cmdoutdvips])
end
[~,cmdoutps2pdf] = system(['ps2pdf ' psdoc ' ' pdfdoc ' -sDEVICE=pdfwrite']);
if contains(cmdoutps2pdf,'Error','IgnoreCase',true)
%Do some kind of check and fix things.
error(cmdoutps2pdf)
end

根据@Cris Luengo关于移动到pdflatex而不是使用latex/dvips/ps2pdf命令的建议,这是可行的。以下是适用于系统命令的示例代码。

[~,pdflatexout] = system(['pdflatex.exe -output-directory ' strrep(exampledir,'','/') ' ' texdoc]);
%Check for errors.
if contains(pdflatexout,'Sorry, but pdflatex.exe did not succeed.') || ...
contains(pdflatexout,'error','IgnoreCase',true)
%Deliver the error code.
error(pdflatexout)
end

我将它放在原始代码中fclose(fileID)之后,并删除了该行之后的所有内容。

相关内容

  • 没有找到相关文章

最新更新