章節中的陰影字母

章節中的陰影字母

這週我寫了這個字:

\documentclass{book}
\usepackage{xcolor}

\definecolor{Monvert}{RGB}{229,36,36}

\definecolorseries{MONVERT}{rgb}{last}{Monvert}{black}

\resetcolorseries[15]{MONVERT}
\begin{document}

\chapter{Test}
  \Huge
\textcolor{MONVERT!!+}{P}\textcolor{MONVERT!!+}{s}\textcolor{MONVERT!!+}{y}\textcolor{MONVERT!!+}{c}\textcolor{MONVERT!!+}{h}\textcolor{MONVER
T!!+}{o}\textcolor{MONVERT!!+}{P}\textcolor{MONVERT!!+}{a}\textcolor{MONVERT!!+}{t}\textcolor{MONVERT!!+}{h}\textcolor{MONVERT!!+}{o}\textcolor{MONVERT!!+}{l}\textcolor{MONVERT!!+}{o}\textcolor{MONVERT!!+}{g}\textcolor{MONVERT!!+}{y}
\end{document}   

我的問題是:如果我想用相同的方式自訂章節和部分,我該怎麼做?任何見解或解決方案將不勝感激

答案1

初步版本,無\section零件。如果章節標題包含巨集而不是不可擴展的材料,它很可能會失敗。

\documentclass{book}
\usepackage{xcolor}

\definecolor{Monvert}{RGB}{229,36,36}

\definecolorseries{MONVERT}{rgb}{last}{Monvert}{black}

\resetcolorseries[50]{MONVERT}

\usepackage{xpatch}

\usepackage{pgffor}

\usepackage{xstring}

\usepackage{xparse}

\NewDocumentCommand{\shadetitle}{+m}{%
  \StrLen{#1}[\titlelength]
  \foreach \x in {1,...,\titlelength} {%
    \textcolor{MONVERT!!+}{\StrChar{#1}{\x}}%
  }%
}

\makeatletter
\xpatchcmd{\@makechapterhead}{\Huge \bfseries #1\par\nobreak}{\Huge \bfseries \shadetitle{#1}\par\nobreak}{}{}
\makeatother
\begin{document}



\chapter{Test it with a very long title}
  \Huge
  \textcolor{MONVERT!!+}{P}\textcolor{MONVERT!!+}{s}\textcolor{MONVERT!!+}{y}\textcolor{MONVERT!!+}{c}\textcolor{MONVERT!!+}{h}\textcolor{MONVERT!!+}{o}\textcolor{MONVERT!!+}{P}\textcolor{MONVERT!!+}{a}\textcolor{MONVERT!!+}{t}\textcolor{MONVERT!!+}{h}\textcolor{MONVERT!!+}{o}\textcolor{MONVERT!!+}{l}\textcolor{MONVERT!!+}{o}\textcolor{MONVERT!!+}{g}\textcolor{MONVERT!!+}{y}
\end{document}   

在此輸入影像描述

編輯一些改進版本

\documentclass{book}
\usepackage{xcolor}

\definecolor{Monvert}{RGB}{229,36,36}

\definecolorseries{MONVERT}{rgb}{last}{Monvert}{black}

\resetcolorseries[100]{MONVERT}

\usepackage{xpatch}
\usepackage{pgffor}
\usepackage{xstring}
\usepackage{xparse}
\usepackage{blindtext}

\NewDocumentCommand{\shadetitle}{+m}{%
  \StrLen{#1}[\titlelength]%
  \foreach \x in {1,...,\titlelength} {%
    \textcolor{MONVERT!!+}{\StrChar{#1}{\x}}%
  }%
}

\makeatletter

%\xpatchcmd{\@ssect}{\hskip #1\relax #5}{#4{\hskip #1\relax \shadetitle{#5}}}{\typeout{Patch success}}{\typeout{Patch failure}}

% Does not work so far :-(
%\xpatchcmd{\@ssect}{\@svsechd{#4{\hskip #1\relax #5}}}{\@svsechd{#4{\hskip #1\relax \shadetitle{#5}}}}{\typeout{Patching ssect}}{}


% Patching the section header command
\xpatchcmd{\@sect}{\def\@svsechd{%
    #6{\hskip #3\relax
      \@svsec #8}%
  }}{%
    \def\@svsechd{%
      #6{\hskip #3\relax
        \@svsec \shadetitle{#8}}%
    }}{}{}
\xpatchcmd{\@sect}{\interlinepenalty \@M #8\@@par}{          \interlinepenalty \@M \shadetitle{#8}\@@par}{}{}
\xpatchcmd{\@makechapterhead}{\Huge \bfseries #1\par\nobreak}{\Huge \bfseries \shadetitle{#1}\par\nobreak}{}{}
\xpatchcmd{\@makeschapterhead}{\Huge \bfseries #1\par\nobreak}{\Huge \bfseries \shadetitle{#1}\par\nobreak}{}{}
\xpatchcmd{\@makechapterhead}{\huge\bfseries \@chapapp\space \thechapter}{\huge\bfseries \shadetitle{\@chapapp}\space \shadetitle{\thechapter}}{}{}

\makeatother
\begin{document}



\chapter{Test it with a very  title}
\section{And a shorter title}
  \Huge
  \textcolor{MONVERT!!+}{P}\textcolor{MONVERT!!+}{s}\textcolor{MONVERT!!+}{y}\textcolor{MONVERT!!+}{c}\textcolor{MONVERT!!+}{h}\textcolor{MONVERT!!+}{o}\textcolor{MONVERT!!+}{P}\textcolor{MONVERT!!+}{a}\textcolor{MONVERT!!+}{t}\textcolor{MONVERT!!+}{h}\textcolor{MONVERT!!+}{o}\textcolor{MONVERT!!+}{l}\textcolor{MONVERT!!+}{o}\textcolor{MONVERT!!+}{g}\textcolor{MONVERT!!+}{y}

\chapter*{Some other chapter}
\section*{Some other section}
\end{document}   

在此輸入影像描述

我很清楚,這只是一個駭客行為。當然,一定有更好的方法。最有可能的是,最好對標記進行著色,而不是嘗試識別輸入中的每個字元。

相關內容