4

I'm creating a style file to use as a template for submissions to a specific journal. That journal requires a short title in the header. I could easily hard-code such a short title in the style file itself, following the answer to Page header alternatively author name and short title, but that's clearly not a good solution in this case, as I would need to change the template .sty file for every document I write. Any suggestions how I can do this? Dummy lines with \shorttitle are commented out in the MWE below.

\documentclass{article}
\usepackage{lipsum,filecontents}
\begin{filecontents}{mystyle.sty}
%% My package starts here
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mystyle}
\usepackage{titling}
\usepackage{fancyhdr}
    \pagestyle{fancy}
    \fancyhf{}
    \fancyhead[R]{\thetitle\ \thepage} % Instead of this
%   \fancyhead[R]{\theshorttitle\ \thepage} % do something like this
\endinput
%% And ends here
\end{filecontents}
\usepackage{mystyle}

\begin{document}
\title{This is my long title}
%\shorttitle{This is my short title} % This short title should not be printed here - only in the header
\maketitle
\lipsum[1-6]
\end{document}
Sverre
  • 20,729

2 Answers2

7

It seems you can do what you want by defining a command in the .sty such as

\newcommand\@shorttitle{}
% define \theshorttitle to what is given
\newcommand\shorttitle[1]{\renewcommand\@shorttitle{#1}}

You can then use \@shorttitle in the .sty file in place of where you need it (e.g. defining the header; just use \@shorttitle instead of \theshorttitle). You can set the short title from your main document as you suggest, with

\shorttitle{This is my short title}

Hope this helps.

phfaist
  • 596
  • Usually, commands beginning with \the... indicate that they are purposed counter output formatting, not for titles itself. And the usage of \def is not necessary here where the safer \newcommand or \renewcommand would suffice. –  Apr 23 '14 at 15:12
  • @ChristianHupfer If phfaist's suggestion is essentially correct, though, please edit the answer to reflect your objections. – Sverre Apr 23 '14 at 15:17
  • @Sverre: They are essentially correct, I wanted to indicate rather a matter of style, not more or less. –  Apr 23 '14 at 15:19
  • 1
    @ChristianHupfer I understood that. Yet, please edit his answer so that it ends up being a suggestion I (and others who read this) can plug into a preamble or .sty file. – Sverre Apr 23 '14 at 15:20
  • @Sverre: I could (possible) edit his answer, but this is not my task. –  Apr 23 '14 at 15:21
  • I just used \theshorttitle because Sverre referenced that particular command in his example. I agree, something like \@shorttitle is more appropriate; I've edited my answer. – phfaist Apr 23 '14 at 15:25
  • I think it also would be better to avoid using \def, but I don't know exactly what needs changing when using \(re)newcommand instead. Anyone? – Sverre Apr 23 '14 at 16:41
  • There you go. I'm not sure which advantages \newcommand gives here over \def (e.g. in LaTeX itself the \title{} command is implemented with \def, see <texlive-dir>/tex/latex/base/latex.ltx), but I'm happy to hear if I'm wrong. – phfaist Apr 24 '14 at 08:37
5

In the amsart class, the command \title has an optional parameter which does exactly that:

\title[This is my short title]{This is my long title}

Maybe copy their implementation?