13

I have made a fancy title page using titleformat and tikz, but I now face the following problem. The \chaptertitlename command returns \chaptername or \appendixname. I would like to spread the letters of the text returned by \chaptertitlename to a given length.

For example I would like to be able to call: \mycommand{\chaptertitlename}{3cm} in order to space the letters of \chaptertitlename on a total length of 3cm. How to do that ?

EDIT: This example illustrates a nearly working thing:

\documentclass[11pt]{article}
\usepackage{fontspec}
\newcommand{\spreadletters}[2]{\makebox[#2][s]{#1}}

\begin{document}
\spreadletters{m y w o r d}{3cm}
\end{document}

How to convert myword to m y w o r d ?

EDIT 2: Can someone explain me why the first work and not the second one (and how to make it work) ?

\documentclass[11pt]{book}
\usepackage{fontspec}
\usepackage{seqsplit}
\usepackage[explicit]{titlesec}

\newcommand{\spreadletters}[2]{\makebox[#2][s]{\seqsplit{#1}}}

\begin{document}
\chapter{First}
\spreadletters{myword}{10cm} \linebreak % This is working
\spreadletters{\chaptertitlename}{10cm} % This is not working
\end{document}
7
  • 3
    It's difficult to gauge whether this would work, but try adding \usepackage{seqsplit} to your document preamble and use \makebox[3cm][s]{\expandafter\seqsplit\expandafter{\chaptertitlename}} where you need it.
    – Werner
    Commented Apr 5, 2014 at 7:09
  • Could you please add a minimum (working) example to your post? It would help the solvers a lot to actually start working on it. Otherwise, we are guessing how your document looks like.
    – Malipivo
    Commented Apr 5, 2014 at 8:20
  • 1
    \renewcommand{\chaptername}{\protect\makebox[3cm][s]{C h a p t e r}}
    – egreg
    Commented Apr 5, 2014 at 8:31
  • @egreg Is there a way to automatically convert Chapter to C h a p t e r ?
    – Vincent
    Commented Apr 5, 2014 at 8:39
  • 1
    @Vincent What's the problem in just typing that command in the preamble?
    – egreg
    Commented Apr 5, 2014 at 8:45

3 Answers 3

16

Using Paul Stanley's answer to your question Conditions for fancy chapter title, here's a way:

\documentclass{book}
\usepackage[explicit]{titlesec}
\usepackage{lipsum}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\newcommand{\chaptercolor}{blue}
\usepackage{tikz}
\usepackage{xparse}

\titleformat{\chapter}[display]
  {\normalfont\filcenter}
  {\tikz[remember picture,overlay]
   {\node[fill=\chaptercolor,%<--- Not hardcoded color
       font=\sffamily\fontsize{96}{72}\bf\selectfont\color{white},anchor=north east, 
       minimum width=3cm, 
       minimum height=3.4cm] 
       at ([xshift=-1cm,yshift=-1cm]current page.north east) 
         (numb) {\thechapter};
     \node[rotate=90,
           anchor=south,
           inner sep=0pt,
           font=\Huge\sffamily]
       at (numb.west) {\SPREAD\chaptertitlename};%<-- Not hardcoded "CHAPTER"
  }}
  {20pt}
  {\Huge\bfseries\color{\chaptercolor}#1}%< Not hardcoded color
  [\vskip10pt\Large\bfseries***]

\ExplSyntaxOn
\NewDocumentCommand{\SPREAD}{m}
 {% full expand the argument
  \vincent_spread:f { #1 }
 }
\cs_new_protected:Npn \vincent_spread:n #1
 {% with \tl_map_inline:nn we insert \hfil between letters; a final \unskip kills the last \hfil
  \makebox[3.4cm][s]{\tl_map_inline:nn { #1 } { ##1 \hfil } \unskip}
 }
\cs_generate_variant:Nn \vincent_spread:n { f }
\ExplSyntaxOff


\begin{document}

\frontmatter

\chapter{Preface}

\lipsum

\mainmatter\renewcommand{\chaptercolor}{red}

\chapter{Chapter}

\lipsum

\appendix\renewcommand{\chaptercolor}{green!40!black!60}

\chapter{Appendix Something}

\end{document}

enter image description here enter image description here

1
  • Is there a way to implement \SPREAD without expl3? Commented Dec 21, 2017 at 8:50
2

I enclose a small Lua snippet which add spaces after letters except commas, colons and semicolons. It adds space after spaces. This is a small example.

%! lualatex mal-words.tex
\documentclass[11pt]{article}
\pagestyle{empty}
%\newcommand{\spreadletters}[2]{\makebox[#2][s]{#1}} % 1. approach
%\usepackage{seqsplit} % 2. approach
\usepackage{luatextra}
\usepackage{luacode}

\begin{document}
\frenchspacing
%\spreadletters{my word}{3cm}\par% ->m y w o r d % 1. approach
%\makebox[3cm][s]{\expandafter\seqsplit\expandafter{my word}} % 2. approach
\def\malword#1{\directlua{spreadme("#1")}}
%\def\addme{\allowbreak}

\begin{luacode*}
local malset={",", ";", ":"} -- A list of exceptions...

function spreadme(text)
print() -- just an empty line
textlength=string.len(text) -- How many characters we are dealing with?

-- The core cycle...
for i=1,textlength do
if i>1 then bonus="\\enspace " else bonus="" end -- Starting adding spaces...
malletter=string.sub(text,i,i) -- Select only one character...

-- Is a character in the list of exceptions?
for _,v in pairs(malset) do
  if v==malletter then bonus="" break end
end -- of for
tex.sprint(bonus..malletter) -- This is a way without adding \n as in print().
--io.write -- .."\\addme"
end -- of i
end -- of function spreadme
\end{luacode*}

\malword{My first, second and third word!}
\end{document}

mwe

0

with the package graphicx, you can use:

\resizebox{3cm}{!}{\chaptertitlename} 
1
  • 1
    It is a geometric transformation of the box, I wouldn't classify it as a spreading letters situation. :-)
    – Malipivo
    Commented Apr 5, 2014 at 8:44

You must log in to answer this question.

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