[Tex/LaTex] Spread letters to a given length

horizontal alignmentspacing

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}

Best Answer

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

Related Question