# Redefine \title for possibility of a forced line breaking in a predefined place

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}

• 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 Dec 16 '18 at 15:53
• @samcarter Ok, but \hypersetup{pdftitle={\@title}} was just for example. – sergiokapone Dec 16 '18 at 15:58
• 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

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).

• Yes, thank you. This is some solution of my problem. – sergiokapone Dec 16 '18 at 16:10

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}


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}


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}

• How can I force line bracking only one place of document, but not in others? – sergiokapone Dec 16 '18 at 15:49
• @sergiokapone can you give an example where you want it without linebreak? You could specify an optional shorttitle – samcarter Dec 16 '18 at 15:51
• It is hard to give an example, because in some places of document the \@title should be in one line, but in some another it should to break in predefined place. Fo example, if title looks like \title{The World Big Idea: with small corrections}. The some places need to be break The World Big Idea: \\ with small corrections – sergiokapone Dec 16 '18 at 15:54
• @sergiokapone If it is hard to give an example, it is also hard to answer your question. In your question you only mentioned that you need one line for the pdf meta data and this is what I covered in my answer – samcarter Dec 16 '18 at 15:56
• @sergiokapone Off-topic: Don't use \\ for line breaks – samcarter Dec 16 '18 at 15:57