将字母分散到指定的长度?

将字母分散到指定的长度?

titleformat我已经使用和制作了一个精美的标题页tikz,但现在面临以下问题。\chaptertitlename命令返回\chaptername\appendixname。我想将 返回的文本的字母分散\chaptertitlename到给定的长度。

例如,我希望能够调用: \mycommand{\chaptertitlename}{3cm}为了将字母间隔开,\chaptertitlename总长度为3cm。怎么做?

编辑:这个例子说明了一件几乎可以正常工作的事情:

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

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

如何轉換mywordm y w o r d

编辑 2:有人能向我解释为什么第一个有效而第二个无效(以及如何使其有效)吗?

\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}

答案1

使用 Paul Stanley 的回答来回答你的问题精美章节标题的条件,这里有一种方法:

\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}

在此处输入图片描述 在此处输入图片描述

答案2

我附上了一个小的 Lua 代码片段,它在除逗号、冒号和分号之外的字母后添加空格。它在空格后添加空格。这是一个小例子。

%! 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}

姆韦

答案3

使用 graphicx 包,您可以使用:

\resizebox{3cm}{!}{\chaptertitlename} 

相关内容