3

When we use the macro \title{Some Title}, the macro just assign the Some Text to another internal one \@title as defined in latex.ltx:

\def\title#1{\gdef\@title{#1}}

We can use the \@title in various places of document, for example, in \hypersetup{pdftitle={\@title}} for pdf-meta, or in some another places of an document. For example, if title looks like

\title{The World Big Idea: with small corrections}.

The some places need to be shown as The World Big Idea: line break here with small corrections. But in some another places it should stand still in a single line.

How to redefine \title or possible, any other good ideas?

\documentclass[]{article}

\title{Some Title}

    \begin{document}

    \makeatletter
        \@title % One can use \@title in different places
    \makeatother

    \makeatletter 
        %\@title Here one need to use the same text, but with line breack between "Same" and "Tile"
        Some\\Title
    \makeatother
\end{document}
sergiokapone
  • 5,578
  • 1
  • 16
  • 39
  • 1
    I guess this depends on what uses for \@title you have in mind. – moewe Dec 16 '18 at 15:48
  • @moewe I use the \@title in \hypersetup{pdftitle={\@title}}. Here it shoul be in single line, but in titlepage I need to breack the line in some place. – sergiokapone Dec 16 '18 at 15:51
  • @sergiokapone Off-topic\hypersetup{pdftitle={\@title}} is unnecessary, the title is used per default for the pdf meta data – samcarter_is_at_topanswers.xyz Dec 16 '18 at 15:53
  • @samcarter Ok, but \hypersetup{pdftitle={\@title}} was just for example. – sergiokapone Dec 16 '18 at 15:58
  • 2
    Mhhh, \@title is one macro and it can obviously only have one definition at a time. You could write something like \title{Some \mypossiblenewline Title} and redefine the meaning of \mypossiblenewline from context to context. But I think it would be tricky to have one place of definition for \@title and several possible outcomes. – moewe Dec 16 '18 at 16:04

3 Answers3

5

Here is a naive idea.

Use a placeholder in the definition of \@title via \title and redefine that placeholder variable as required by the context.

\documentclass[]{article}

\newcommand*{\mypossiblenewline}{\newline}
\title{Some \mypossiblenewline Title}

\begin{document}
\makeatletter
  \@title
\makeatother

\makeatletter 
  \begingroup
  \renewcommand*{\mypossiblenewline}{}
  \@title
  \endgroup
\makeatother
\end{document}

You could wrap that up in one command, for example

\makeatletter
\DeclareRobustCommand*{\sergiotitlenobreak}{%
  \begingroup
  \renewcommand*{\mypossiblenewline}{}%
  \@title
  \endgroup
}
\DeclareRobustCommand*{\sergiotitlewithbreak}{%
  \begingroup
  \renewcommand*{\mypossiblenewline}{\newline}%
  \@title
  \endgroup
}
\makeatother

but that would not be expandable (which could be a problem depending on the context).

moewe
  • 175,683
3

Save two more variables, for lack of creativity I named them \realtitle and \inlinetitle. In the second one, \\ tokens are substituted with spaces, first removing spaces at either side of \\. Thus it is immaterial if you input any of

\title{Some Title \\ with line breaks}
\title{Some Title\\ with line breaks}
\title{Some Title\\with line breaks}

Here's the code.

\documentclass{article}
\usepackage{xparse}
\usepackage{hyperref}

\ExplSyntaxOn
\RenewDocumentCommand{\title}{m}
 {
  \tl_gset:cn { @title } { #1 } % needed by \maketitle
  \tl_gset:Nn { \realtitle } { #1 }
  \seq_set_split:Nnn \l_tmpa_seq { \\ } { #1 }
  \tl_gset:Nx \inlinetitle { \seq_use:Nn \l_tmpa_seq { ~ } }
 }
\ExplSyntaxOff

\author{author}
\title{Some Title \\ with line breaks}

\hypersetup{
  pdftitle=\inlinetitle
}


\begin{document}

\maketitle

The title of this document is ``\inlinetitle''.

\begin{center}
\realtitle
\end{center}

\end{document}

enter image description here

You can also cope with \\[<dimen>]:

\documentclass{article}
\usepackage{xparse}
\usepackage{hyperref}

\ExplSyntaxOn
\RenewDocumentCommand{\title}{m}
 {
  \tl_gset:cn { @title } { #1 } % needed by \maketitle
  \tl_gset:Nn { \realtitle } { #1 }
  \tl_set:Nn \inlinetitle { #1 }
  \regex_replace_all:nnN { \s* \c{\\}(\[[^\]]*\])+ \s* } { \  } \inlinetitle
 }
\ExplSyntaxOff

\author{author}
\title{Some Title \\[1ex] with line breaks \\[1ex] and added spacing}

\hypersetup{
  pdftitle=\inlinetitle
}


\begin{document}

\maketitle

The title of this document is ``\inlinetitle''.

\begin{center}
\realtitle
\end{center}

\end{document}

enter image description here

egreg
  • 1,121,712
2

Different approach: instead of redefining \@title, you can use \texorpdfstring to specify two versions, one without linebreaks which will be used in the pdf meta data and one with linebreak to be displayed in the pdf

\documentclass[]{article}

\usepackage{hyperref}

\title{\texorpdfstring{Some\newline Title}{Some title}}

    \begin{document}

    \makeatletter
        \@title % One can use \@title in different places
    \makeatother

\end{document}