如何在源 LaTeX 文件中设置生成的 Postscript® 文件的标题?

如何在源 LaTeX 文件中设置生成的 Postscript® 文件的标题?

有时候,我必须将源 .tex 文件编译为 .ps 文件。(原因可能有所不同:例如,由于一定数量的 pstricks 重度遗留代码,或者某些旧打印机 CUPS 过滤器突然无法正确处理提交的 PDF,或者 IEEE 期刊仍然要求我提供 Postscript 文件等。)这些 Postscript® 文件有一个标题注释,通常如下所示:

%%Title: main.dvi

其中“main”来自我的输入文件“main.tex”。但是,我通常希望在其中添加一些更有意义的内容,以便查看者(例如gv或)evince能够解析并在窗口标题中正确显示,以及在被询问文件属性时正确显示。因此,在运行latex+之后dvips,我使用 Makefile 中的脚本对 Postscript® 文件进行后处理sed。这可以通过以下命令完成,例如

$(PSOBJECTS): \
%.ps: %.dvi Makefile
    dvips -o $*.ps $<
    sed -i 's/^%%Title: $<$$/%%Title: This is a very long title in one line without line breaks/' $@
    chmod a+r $@

这种方法的缺点是,现在标题是两次:在 tex 文件和 Makefile 中。因此,当您在一个地方更改标题时,您经常会忘记在另一个地方更改标题。我想知道是否可以通过一些巧妙的命令在 tex 文件(这将是设置标题的唯一位置)中设置 Postscript® 文件的标题。

开始的示例代码:

\RequirePackage{ifpdf}
\RequirePackage{ifxetex,ifluatex}
\newif\ifxetexorluatex
\ifxetex
  \xetexorluatextrue
\else
  \ifluatex
    \xetexorluatextrue
  \else
    \xetexorluatexfalse
  \fi
\fi

\documentclass{book}
\usepackage{relsize}
\newcommand{\bookTitleInOneLine}{This is a very long title in one line without line breaks}
\newcommand{\bookTitleWithLineBreaks}{This is a very long title\\occupying several lines\\with line breaks\\at meaningful positions}
\newcommand{\authorList}{John Doe, Sally Sixpack, Joe Bloggs, and John Smith}
\ifxetexorluatex
  \usepackage[unicode,pdftitle={\bookTitleInOneLine},hidelinks,pdfauthor={\authorList}]{hyperref}%%% Setting basic meta data for the PDF
\else
\ifpdf
    \usepackage[unicode,pdftitle={\bookTitleInOneLine},hidelinks,pdfauthor={\authorList}]{hyperref}%%% Setting basic meta data for the PDF
  \else
    \usepackage[unicode,hidelinks]{hyperref}
     %%% Here, we'd ideally set the title for the dvi+postscript
  \fi
\fi
\begin{document}
\title{\larger[1.999]\bookTitleWithLineBreaks}
\author{\authorList}
\maketitle
\end{document}

答案1

不,不可能。是的,确实可以!(虽然不是在 LaTeX 中)

Akira Kakuto 制作改变dvips在命令行中添加一个-title选项,用于设置后面的字符串%%Title

     if (*titlename)
        fprintf(bitfile, "%%%%Title: %s\n", titlename);
     else if (*iname)
        fprintf(bitfile, "%%%%Title: %s\n", xbasename(iname));

其中titlename来自-title选项。虽然无法从 LaTeX 代码中设置它,但这已经让您不必使用 来对 DVI 文件进行后处理sed


但请注意,正如 Ulrike 在评论中所说hyperref文件中有标题ps,但位于不同位置。如果您编译文档:

\documentclass{article}
\usepackage[pdftitle=Title]{hyperref}
\begin{document}
a
\end{document}

ps在文本编辑器中打开生成的文件,您将在文件末尾附近看到:

SDict begin [/Producer (dvips + Distiller)/Title (Title)/Subject ()/Creator
(LaTeX with hyperref)/Author ()/Keywords () /DOCINFO pdfmark end

在上述更改之前(2020-11-29),该%%Title字符串被硬编码为文件名。我的旧答案,记录如下:

%%Title字符串写在output.cdvips

         fprintf(bitfile, "%%%%Title: %s\n", iname);

并且inamedvi(第 127 行dvips.c):

char *iname;                 /* dvi file name */

您必须请求更改dvips才能获取文档元数据并将其传递给字符串%%Title。我认为,如果您更改 makefile 以自动将标题添加到 LaTeX 文件,或者反过来,可能会更容易。

相关内容