Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Is there a way to get n shorter strings out of a long one in Latex? Let's say I have something like \title{A very long title} and I want it to be broken into two pieces such that I can later use colorbox{red}{A very} and \color{blue}{long title}. The break point should be calculated through a distance provided by the user.

share|improve this question
    
Out of \title, yes, but you'll have to store the content first/before \maketitle since most document classes remove \@title - the placeholder macro. By distance, you mean characters? –  Werner Oct 22 '12 at 14:11
    
@Werner by distance, I mean "if it is longer than 8cm break the title". Here the use of the \title{} command was just an example. What I have in mind is related to the \chapter{} command. –  pluton Oct 22 '12 at 14:57
    
I don't think that, even if feasible in general, such an automatic procedure is good: breaking a title requires semantic considerations. For instance The story of\\my life would probably be better split as The story\\of my life. –  egreg Oct 22 '12 at 21:04
    
ok, so the user could indicated where the title should be broken but the two (sub-)strings are still desired. –  pluton Oct 23 '12 at 0:00
    
in which case a new command \chapter[optional]{substring1}{substring2} should be designed? –  pluton Oct 23 '12 at 0:11
show 1 more comment

1 Answer

up vote 4 down vote accepted

I don't think you need anything special for the \chapter command:

\chapter[The real long title]
  {\textcolor{red}{The real}\\
   \textcolor{green}{long title}}

is all you need. You can abstract it with new commands:

\documentclass{book}
\usepackage{xcolor}
\usepackage{xparse}
\newcommand{\firstchapline}[1]{\textcolor{red}{#1}}
\newcommand{\secondchapline}[1]{\textcolor{green}{#1}}

\makeatletter
\NewDocumentCommand{\coloredchapter}{som}
 {\IfBooleanTF{#1}
   {\chapter*{#3}}
   {\IfNoValueTF{#2}
     {\make@coloredchapter{#3}}
     {\chapter[#2]{#3}}%
   }%
 }
 \newcommand{\make@coloredchapter}[1]{%
   \begingroup
   \let\firstchapline\@firstofone
   \let\secondchapline\@firstofone
   \let\\\space
   \protected@edef\x{\endgroup\noexpand\chapter[#1]}%
   \x{#1}}
\makeatletter

\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\coloredchapter{\firstchapline{A really}\\\secondchapline{long title}}
\coloredchapter[Short title]{\firstchapline{A really}\\\secondchapline{long title}}
\coloredchapter*{\firstchapline{A really}\\\secondchapline{long title}}
\end{document}

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.