4

I'm trying to create a macro for generating a title page for manuals. In the simplest case the title should look like

{\huge
\textbf{\productname{} \manualtitle{}}
}

But I would like to have the following rules applied for line-breaks:

  1. If the whole title fits into one line, do not break it.
  2. If manualtitle fits into one line but not together with productname, break between the two.
  3. If manualtitle is longer than the line, break after productname and break manualtitle automatically but without hyphens.

I know how to get parts of it to work manually using \newline and the hyphenrules and sloppypar environments, but I can't find a solution that fulfills all three requirements.

Minimal example as requested:

\documentclass[a4paper, 12pt]{report}

\usepackage[paper=a4paper,
            left=25mm,
            right=25mm,
            top=25mm,
            bottom=25mm,
            showframe]{geometry}

\usepackage[english]{babel}

\setlength\parindent{0pt}

\newcommand{\product}{PRODUCT}
\newcommand{\manualtitle}{User Manual}

\newcommand{\printtitle}{
  \vspace{2em}

  {\huge
    \textbf{\product{} \manualtitle{}}
  }
}

\begin{document}

\printtitle

\renewcommand{\manualtitle}{More Important User Manual}
\printtitle

\renewcommand{\manualtitle}{Even Way More Important User Manual}
\printtitle

\end{document}

Desired output:

Desired output

5
  • Is the title being typeset in a center context?
    – egreg
    Oct 27, 2016 at 13:13
  • Can you make an actual example so that we can test solutions? Oct 27, 2016 at 13:14
  • @egreg no, but flushleft might be an option
    – Hurzelchen
    Oct 27, 2016 at 13:23
  • @samcarter edited the question accordingly
    – Hurzelchen
    Oct 27, 2016 at 13:23
  • this tactic is manual, but it's reliable -- use the "tie" ~ form of space to keep words together. works quite nicely for titles that are defined as ragged right. (on your last example, i'd use it for "User~Manual"..) Oct 28, 2016 at 13:56

1 Answer 1

3

This is a possible solution:

\documentclass[a4paper, 12pt]{report}

\usepackage[paper=a4paper,
            left=25mm,
            right=25mm,
            top=25mm,
            bottom=25mm,
            showframe]{geometry}

\usepackage[english]{babel}

\setlength\parindent{0pt}

\newcommand{\product}{PRODUCT}
\newcommand{\manualtitle}{User Manual}

\newcommand{\printtitle}{%
  \par
  \vspace*{2em}%
  \par\begingroup\raggedright
  \huge\bfseries
  \sbox0{\let\\\relax\product{} \manualtitle}%
  \ifdim\wd0>\linewidth
    \product\\
    \manualtitle
    \par
  \else
    \box0
  \fi
  \endgroup
}

\begin{document}

\printtitle

\renewcommand{\manualtitle}{More Important User Manual}
\printtitle

\renewcommand{\manualtitle}{Even Way More Important User Manual}
\printtitle

\renewcommand{\manualtitle}{Even Way More Important \\ User Manual}
\printtitle

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .