帮助在章节标题中添加颜色选项

帮助在章节标题中添加颜色选项

我正在尝试创建一个选项,以便轻松更改章节标题中的颜色条。此代码可以更改条的颜色,但会破坏格式。所有章节文本都应与第一章页面的右边距对齐。提前感谢任何建议。詹姆斯<code>在此处输入图片描述</code>

\documentclass[twoside]{memoir}
\usepackage{blindtext, fancyhdr}
\pagestyle{fancy}
\usepackage{graphics}
\usepackage{titlesec}
\usepackage{etoolbox}

\usepackage[dvipsnames]{xcolor}
\colorlet{LightRubineRed}{RubineRed!70!}
\colorlet{Mycolor1}{green!10!orange!90!}
\definecolor{Mycolor2}{HTML}{00F9DE}

%% below is a poor close color option, does what it should but can't use \titleformat
\renewcommand{\colorChapter}[2]{\chapter[Black display]
    {\normalfont\Large\raggedleft}
    {\MakeUppercase{#2}%
    \rlap{ \resizebox{!}{1.2cm}{\thechapter} \textcolor{#1}{\rule{15cm}{1.2cm}} } }
    {10pt}{\Huge}}


\titlespacing*{\chapter}{0pt}{30pt}{20pt}
%% \addtolength{\evensidemargin}{.875in}
\addtolength{\evensidemargin}{.999in}
\addtolength{\textwidth}{-1.00in}

\begin{document}
\colorChapter{Blue}{What are Family Values}

%% ===================================================
%% DIFFERENT CODE THAT PRODUCES THE DESIRED FORMAT
%% The following code produces the desired format, but it can not be used to change %% colors between different chapters.
\documentclass[twoside]{memoir}
\usepackage{blindtext, fancyhdr}
\pagestyle{fancy}
\usepackage{graphics}
\usepackage{titlesec}
\usepackage{etoolbox}

\usepackage[dvipsnames]{xcolor}
\colorlet{LightRubineRed}{RubineRed!70!}
\colorlet{Mycolor1}{green!10!orange!90!}
\definecolor{Mycolor2}{HTML}{00F9DE}

%% This works but can not change color of bar from chapter to chapter
\titleformat{\chapter}[display]
    {\normalfont\Large\raggedleft}
    {\MakeUppercase{\chaptertitlename}%
    \rlap{ \resizebox{!}{1.2cm}{\thechapter} \textcolor{Blue}{\rule{15cm}{1.2cm}} } }
    {10pt}{\Huge}

\titlespacing*{\chapter}{0pt}{30pt}{20pt}
%% \addtolength{\evensidemargin}{.875in}
\addtolength{\evensidemargin}{.999in}
\addtolength{\textwidth}{-1.00in}


\usepackage{lipsum}
\begin{document}
\chapter{What are Family Values}

\epigraph{\textit{We can choose our values. }}{Dr. Phil's Dad}
\section{First section}
\blindtext
\section{Second Section}
Praesent imperdiet mi nec ante. Donec ullamcorper,
felis non sodales commodo, lectus velit ultrices augue, a dignis-
sim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut,
ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis
fringilla tristique neque. Sed interdum libero ut metus. Pellen-
tesque placerat. Nam rutrum augue a leo. Morbi sed elit sit
amet ante lobortis sollicitudin. Praesent blandit blandit mau-
ris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a,
turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna
dictum turpis accumsan semper.

Praesent imperdiet mi nec ante. Donec ullamcorper,
felis non sodales commodo, lectus velit ultrices augue, a dignis-
sim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut,
ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. 

Duis fringilla tristique neque. Sed interdum libero ut metus. Pellen-
tesque placerat. Nam rutrum augue a leo. Morbi sed elit sit
amet ante lobortis sollicitudin. Praesent blandit blandit mau-
ris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a,
turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna
dictum turpis accumsan semper.

\end{document}

代码可以产生所需的格式,但不能在章节之间改变颜色

答案1

利用该类memoir,您可以定义自己的章节样式,而不需要包titlesec

您尝试实现的风格与预定义veelo风格非常相似。

所以只要稍微修改一下就可以了。我们将定义一个新的章节样式colored

\makeatletter
\newcommand{\chaptercolor}{}
\makechapterstyle{colored}{%
  \setlength{\afterchapskip}{40pt}
  \renewcommand*{\chapterheadstart}{\vspace*{40pt}}
  \renewcommand*{\afterchapternum}{\par\nobreak\vskip 25pt}
   \renewcommand*{\chapnamefont}{\normalfont\Large\flushright}
   \renewcommand*{\chapnumfont}{\normalfont}
   \renewcommand*{\chaptitlefont}{\normalfont\Huge\flushright}
   \renewcommand*{\printchaptername}{%
     \chapnamefont\MakeTextUppercase{\@chapapp}}
   \renewcommand*{\chapternamenum}{}
  \setlength{\beforechapskip}{1.2cm}%  \numberheight
  \setlength{\midchapskip}{\paperwidth}% \barlength
  \addtolength{\midchapskip}{-\textwidth}
   \renewcommand*{\printchapternum}{%
     \makebox[0pt][l]{%
       \hspace{.8em}%
       \resizebox{!}{\beforechapskip}{\chapnumfont \thechapter}%
       \hspace{.8em}%
       \textcolor{\chaptercolor}{\rule{\midchapskip}{\beforechapskip}}%
     }%
   }%
   \makeoddfoot{plain}{}{\thepage}{}}
\makeatother

并通过命令在文档中使用它

\chapterstyle{colored}

现在我们来定义一个新命令\colorchapter

\newcommand{\colorchapter}[3][]{\renewcommand{\chaptercolor}{#3}\chapter[#1]{#2}}

以这种方式使用(例如):

\colorchapter{What are Family Values}{Blue}

使用以下 MWE

\documentclass[twoside]{memoir} 
\usepackage{blindtext} 
\usepackage{graphicx} 
\usepackage{etoolbox}

\usepackage[dvipsnames]{xcolor} 
\colorlet{LightRubineRed}{RubineRed!70!} 
\colorlet{Mycolor1}{green!10!orange!90!} 
\definecolor{Mycolor2}{HTML}{00F9DE}


\makeatletter
\newcommand{\chaptercolor}{}
\makechapterstyle{colored}{%
  \setlength{\afterchapskip}{40pt}
  \renewcommand*{\chapterheadstart}{\vspace*{40pt}}
  \renewcommand*{\afterchapternum}{\par\nobreak\vskip 25pt}
   \renewcommand*{\chapnamefont}{\normalfont\Large\flushright}
   \renewcommand*{\chapnumfont}{\normalfont}
   \renewcommand*{\chaptitlefont}{\normalfont\Huge\flushright}
   \renewcommand*{\printchaptername}{%
     \chapnamefont\MakeTextUppercase{\@chapapp}}
   \renewcommand*{\chapternamenum}{}
  \setlength{\beforechapskip}{1.2cm}%  \numberheight
  \setlength{\midchapskip}{\paperwidth}% \barlength
  \addtolength{\midchapskip}{-\textwidth}
   \renewcommand*{\printchapternum}{%
     \makebox[0pt][l]{%
       \hspace{.8em}%
       \resizebox{!}{\beforechapskip}{\chapnumfont \thechapter}%
       \hspace{.8em}%
       \textcolor{\chaptercolor}{\rule{\midchapskip}{\beforechapskip}}%
     }%
   }%
   \makeoddfoot{plain}{}{\thepage}{}}
\makeatother

\chapterstyle{colored}

\newcommand{\colorchapter}[3][]{\renewcommand{\chaptercolor}{#3}\chapter[#1]{#2}}

%% \addtolength{\evensidemargin}{.875in} 
\addtolength{\evensidemargin}{.999in} 
\addtolength{\textwidth}{-1.00in}

\usepackage{lipsum} 

\begin{document} 

\colorchapter{What are Family Values}{Blue}

\epigraph{\textit{We can choose our values. }}{Dr. Phil's Dad} 

\section{First section} 
\lipsum[1] 
\section{Second Section} 
\lipsum[1] 

\colorchapter{What are Family Values}{LightRubineRed}

\epigraph{\textit{We can choose our values. }}{Dr. Phil's Dad}

\section{First section}
\lipsum[1]
\section{Second Section}
\lipsum[1]

\end{document} 

结果是

在此处输入图片描述

相关内容