3

Every once in a while I have to compile source .tex files into .ps files. (The reasons may differ: e.g., due to certain amount of pstricks-heavy legacy code, or issues of some old-printer–CUPS-filters that suddently do not process submitted PDFs properly, or an IEEE journal still requesting a Postscript file from me, etc.) These Postscript® files have a title comment which usually looks like follows:

%%Title: main.dvi

where "main" comes from my input file "main.tex". However, I usually wish to have something more meaningful there that the viewers such as gv or evince would parse and display properly in their window captions and when being asked about the file properties. Therefore, after running latex+dvips I postprocess the Postscript® file with a sed script from the Makefile. This is done by a command such as

$(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 $@

The downside of this approach is that now the title is present twice: inside the tex file and inside the Makefile. So, when you change the title at one place, you often forget to change the title at another place. I'm wondering whether it is possible to set the title for the Postscript® file from within the tex file (which will be then the only place then where the title will be set) by some clever command(s).

Example code to start with:

\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}
3
  • Do you want to see the title in the window title? Why don't you use then \usepackage[pdftitle=Title,pdfdisplaydoctitle=true]{hyperref}? Apr 3, 2019 at 16:06
  • 1
    If you insert your line instead of \usepackage[unicode,hidelinks]{hyperref}, the title doesn't show up in the postscript file generated by latex+dvips, and the viewer gv still shows "main.dvi" in the window caption when viewing main.ps.
    – user165772
    Apr 3, 2019 at 16:11
  • 1
    Since you are using sed in the makefile (so Linux) you may think to grep the \Title{....} from the tex file, parse the line(s) and add that title inside the ps... all in the makefile...
    – Hastur
    Jul 16, 2020 at 19:19

1 Answer 1

5
+250

No, it's not possible. YES, IT IS! (though not from within LaTeX)

Akira Kakuto made a change to dvips that adds a -title option to the command line that sets the string after %%Title:

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

where titlename comes from the -title option. It is not possible, though, to set this from within your LaTeX code, but this already saves you from having to postprocess your DVI file with sed.


But note that, as Ulrike said in the comment, that with hyperref you do have the title in the ps file, but in a different place. If you compile the document:

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

and open the generated ps file in a text editor, you'll see near the end of the file:

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

Before the aforementioned change (in 2020-11-29), the %%Title string was hardcoded as the file name. My old answer, for the record:

The %%Title string is written in line 1440 of output.c of dvips:

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

and iname is the dvi (line 127 of dvips.c):

char *iname;                 /* dvi file name */

You'd have to request a change in dvips to get the document metadata and pass it to the %%Title string. I think it might be easier if you change your makefile to add the title to the LaTeX file, or the other way around, automatically.

You must log in to answer this question.