Ghostscript ps2pdf在MATLAB中无法正常工作

时间:2018-10-31 20:02:19

标签: matlab latex ghostscript

我有一个.tex文件,我正在尝试从MATLAB环境(2017b)中使用第一个乳胶,然后是dvips,最后是ps2pdf来创建PDF文件。通过TeXWorks运行该PDF文件时会正确创建它,但是当我尝试运行它时,会从ps2pdf发送错误。我不完全确定将其发送到命令行的方式有什么问题。这是我尝试运行的脚本。

    set(groot, 'defaultAxesTickLabelInterpreter','latex'); 
set(groot, 'defaultLegendInterpreter','latex');
set(0,'defaultTextInterpreter','latex');

addpath('C:\Program Files (x86)\MiKTeX\miktex\bin\x64')
exampledir='\\our\server\';
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 ...
    '\setlength\headheight{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]{{\\our\server\mypicture}}}' 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]{{\\our\server\mypicture}}' 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

1 个答案:

答案 0 :(得分:0)

按照@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)之后,并删除了该行之后的所有原始内容。