LaTeX:如何根据回忆录中的章节编号更改颜色?

LaTeX:如何根据回忆录中的章节编号更改颜色?

我想根据章节号更改一些页面元素(规则、标题......)的颜色,例如第一章,颜色为蓝色,第二章,颜色为绿色......并在四章后循环;这是为了与某些公司的徽标颜色相匹配

我尝试创建一些计数器并通过重新定义回忆录类中的命令 \memendofchapterhook 来为每个新章节增加它们 - 该命令在每个新章节之后、在 \chapter 的末尾执行。

尽管如此,它还是不起作用,因为颜色没有跟随计数器的变化

这是我的 tex 文件,计数器在序言中定义,标题规则的颜色也在序言中的标题部分中定义

% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode

% Example of the Memoir class, an alternative to the default LaTeX classes such as article and book, with many added features built into the class itself.

\documentclass[12pt,a4paper,book,openright,notitlepage %,draft %,draft  %si draft, pas d'images ds le doc final
            ]{memoir} % for a short document

\usepackage[T1]{fontenc} %set output encoding 
\usepackage[utf8]{inputenc} % set input encoding to utf8

\usepackage{tcolorbox}

%%CHANGEMENT DE LANGUE
\usepackage[french]{babel} %change la langue pour le français

%%% PAGE DIMENSIONS
% Set up the paper to be as close as possible to both A4 & letter:
\settrimmedsize{297mm}{210mm}{*} % letter = 11in tall; a4 = 210mm wide modifié A4 pur
\setlength{\trimtop}{0pt}
\setlength{\trimedge}{\stockwidth}
\addtolength{\trimedge}{-\paperwidth}
\setlxvchars \setxlvchars[\normalfont]%censé régler la largeur du bloc de caractères pour un nbre optimum de lettres /lignes en fonction de la police
\settypeblocksize{*}{\lxvchars}{1.618} % we want to the text block to have golden proportionals
\setbinding{20pt} %ajoute un espace pour la reliure (cette valeur est soustraite de la largeur théorique de la page)
\setulmargins{50pt}{*}{*} % 50pt upper margins
\setlrmargins{*}{*}{1.618} % golden ratio again for left/right margins
\setheaderspaces{*}{*}{1.618}

\checkandfixthelayout 
% This is from memman.pdf



%%% COULEURS & LOGO
\definecolor{logo_purple}{RGB}{109,20,71}%Définit la couleur POURPRE du logo SPMS
\definecolor{logo_blue}{RGB}{15,115,159}%Définit la couleur BLEUE du logo SPMS
\definecolor{logo_green}{RGB}{129,166,42}%Définit la couleur VERTE du logo SPMS
\definecolor{logo_orange}{RGB}{238,128,15}%Définit la couleur ORANGE du logo SPMS

\newcommand{\logoColor}{\color{logo_blue}}

\newcounter{logoUn}\setcounter{logoUn}{0}
\newcounter{logoDeux}\setcounter{logoDeux}{1}
\newcounter{logoTrois}\setcounter{logoTrois}{2}
\newcounter{logoQuatre}\setcounter{logoQuatre}{3}
\renewcommand{\memendofchapterhook}{%%redéfinition d'une commande (vide) de memoir qui s'exécute *après* la commande \chapter 
    \stepcounter{logoUn}
    \stepcounter{logoDeux}
    \stepcounter{logoTrois}
    \stepcounter{logoQuatre}
    \ifnum \thelogoUn=4 {cas1 \renewcommand{\logoColor}{\color{logo_blue}}\pagestyle{my_page_style}} \fi
    \ifnum \thelogoDeux=4 {cas2 \renewcommand{\logoColor}{\color{logo_green}}\pagestyle{my_page_style}} \fi
    \ifnum \thelogoTrois = 4 {cas3 \renewcommand{\logoColor}{\color{logo_orange}}\pagestyle{my_page_style}} \fi
    \ifnum \thelogoQuatre = 4 {cas4} \fi
    %   \else {cas autre}
    %\fi     

} 

%%% HEADERS & FOOTERS

\copypagestyle{my_page_style}{ruled} %définit un style local modifiable basé sur "ruled"
    \makeheadfootruleprefix{my_page_style}{\logoColor}{} %définit la couleur du trait entre l'en-tête et le texte et entre le texte et le pied de page (N/A pour le style "ruled")
    \makeheadrule{my_page_style}{\textwidth}{4pt} %style de page, longueur de la règle, épaisseur (\normalrulethickness = 0.4pt)





%applique ce style au document
\pagestyle{my_page_style} % try also: empty , plain , headings , ruled , Ruled , companion 


%%% CHAPTERS
\chapterstyle{bianchi} % try also: default , section , hangnum , companion , article, demo, verville, wilsondob, ger, komalike,dash, culver, crosshead, chappell, brotherton,bringhurst, 

%% END Memoir customization



%%% BEGIN DOCUMENT
\begin{document}


\mainmatter %correspond au contenu principal, les pages sont numérotées en chiffres arabes, la numérotation reprend au début. 
        %Attention, les appendices font partie du contenu principal!


\chapter{test}\thispagestyle{my_page_style}
\thelogoUn
\section{test}
\subsection{test}
 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vulputate mauris at nisl tristique, eu pharetra turpis suscipit. Suspendisse sed pellentesque ligula. Maecenas pulvinar mi velit, at lobortis velit faucibus sed. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Ut rhoncus eros maximus nibh semper varius. Proin mollis risus augue, sit amet porta metus aliquet et. Cras sit amet scelerisque lorem. Vivamus rhoncus tincidunt lacus, non malesuada enim sollicitudin non. Sed consectetur ipsum eget bibendum vehicula. Phasellus in laoreet purus, accumsan convallis ex. Ut sed semper ipsum. Nam vitae eros a tortor iaculis varius. Ut eu sem sed arcu venenatis pulvinar. Curabitur augue arcu, tempus in suscipit sit amet, auctor a nisi. In laoreet ornare lacus, in iaculis odio tincidunt ac.

 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vulputate mauris at nisl tristique, eu pharetra turpis suscipit. Suspendisse sed pellentesque ligula. Maecenas pulvinar mi velit, at lobortis velit faucibus sed. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Ut rhoncus eros maximus nibh semper varius. Proin mollis risus augue, sit amet porta metus aliquet et. Cras sit amet scelerisque lorem. Vivamus rhoncus tincidunt lacus, non malesuada enim sollicitudin non. Sed consectetur ipsum eget bibendum vehicula. Phasellus in laoreet purus, accumsan convallis ex. Ut sed semper ipsum. Nam vitae eros a tortor iaculis varius. Ut eu sem sed arcu venenatis pulvinar. Curabitur augue arcu, tempus in suscipit sit amet, auctor a nisi. In laoreet ornare lacus, in iaculis odio tincidunt ac.
 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vulputate mauris at nisl tristique, eu pharetra turpis suscipit. Suspendisse sed pellentesque ligula. Maecenas pulvinar mi velit, at lobortis velit faucibus sed. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Ut rhoncus eros maximus nibh semper varius. Proin mollis risus augue, sit amet porta metus aliquet et. Cras sit amet scelerisque lorem. Vivamus rhoncus tincidunt lacus, non malesuada enim sollicitudin non. Sed consectetur ipsum eget bibendum vehicula. Phasellus in laoreet purus, accumsan convallis ex. Ut sed semper ipsum. Nam vitae eros a tortor iaculis varius. Ut eu sem sed arcu venenatis pulvinar. Curabitur augue arcu, tempus in suscipit sit amet, auctor a nisi. In laoreet ornare lacus, in iaculis odio tincidunt ac.

\par


\chapter{Description}
Valeur : \thelogoUn

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vulputate mauris at nisl tristique, eu pharetra turpis suscipit. Suspendisse sed pellentesque ligula. Maecenas pulvinar mi velit, at lobortis velit faucibus sed. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Ut rhoncus eros maximus nibh semper varius. Proin mollis risus augue, sit amet porta metus aliquet et. Cras sit amet scelerisque lorem. Vivamus rhoncus tincidunt lacus, non malesuada enim sollicitudin non. Sed consectetur ipsum eget bibendum vehicula. Phasellus in laoreet purus, accumsan convallis ex. Ut sed semper ipsum. Nam vitae eros a tortor iaculis varius. Ut eu sem sed arcu venenatis pulvinar. Curabitur augue arcu, tempus in suscipit sit amet, auctor a nisi. In laoreet ornare lacus, in iaculis odio tincidunt ac.

Donec faucibus, elit non sagittis feugiat, justo erat luctus eros, nec hendrerit nulla mi non nisi. Phasellus bibendum, libero a suscipit efficitur, diam ligula congue nunc, feugiat finibus eros urna nec nisi. Vestibulum arcu mi, tempor ac lorem in, eleifend facilisis lacus. Mauris ut velit at leo pellentesque placerat. Donec ut ipsum a elit elementum viverra. Phasellus blandit, tortor et tincidunt congue, nibh ipsum convallis elit, ac rutrum dui odio in quam. Morbi eu lorem sed erat dignissim lacinia. Aliquam rhoncus et tellus et dictum. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Nam ullamcorper dapibus dignissim. Nullam laoreet semper ipsum, ut dapibus ligula vehicula a. Ut nisi massa, iaculis a magna mattis, malesuada facilisis elit. Ut vestibulum dolor ac interdum blandit. Interdum et malesuada fames ac ante ipsum primis in faucibus. Suspendisse porta augue risus, ut tristique dolor commodo lobortis. Donec sagittis at diam quis sodales. Praesent maximus sollicitudin felis, et fermentum massa pretium non.

\chapter{Dummy chapter}
\section{Dummy section}
Nam ullamcorper dapibus dignissim. Nullam laoreet semper ipsum, ut dapibus ligula vehicula a. Ut nisi massa, iaculis a magna mattis, malesuada facilisis elit. Ut vestibulum dolor ac interdum blandit. Interdum et malesuada fames ac ante ipsum primis in faucibus. Suspendisse porta augue risus, ut tristique dolor commodo lobortis. Donec sagittis at diam quis sodales. Praesent maximus sollicitudin felis, et fermentum massa pretium non. 

\end{document}

我不太熟悉 latex 或 tex,所以请不要犹豫,简化你的答案...谢谢

答案1

这个问题出在 的重新定义上\memendofchapterhook。我做了两类更改。首先,我在测试输入\relax后添加了\ifnum,但更重要的是,我使用了\gdef\ 而不是\renewcommand来重新定义\logoColor

\renewcommand{\memendofchapterhook}{%%redéfinition d'une commande (vide) de memoir qui s'exécute *après* la commande \chapter 
    \stepcounter{logoUn}
    \stepcounter{logoDeux}
    \stepcounter{logoTrois}
    \stepcounter{logoQuatre}
    \ifnum \thelogoUn=4\relax {cas1 \gdef\logoColor{\color{logo_blue}}\pagestyle{my_page_style}} \fi
    \ifnum \thelogoDeux=4\relax {cas2 \gdef\logoColor{\color{logo_green}}\pagestyle{my_page_style}} \fi
    \ifnum \thelogoTrois = 4\relax {cas3 \gdef\logoColor{\color{logo_orange}}\pagestyle{my_page_style}} \fi
    \ifnum \thelogoQuatre = 4\relax {cas4} \fi
    %   \else {cas autre}
    %\fi     

}

修订后的 MWE:

% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode

% Example of the Memoir class, an alternative to the default LaTeX classes such as article and book, with many added features built into the class itself.

\documentclass[12pt,a4paper,book,openright,notitlepage %,draft %,draft  %si draft, pas d'images ds le doc final
            ]{memoir} % for a short document

\usepackage[T1]{fontenc} %set output encoding 
\usepackage[utf8]{inputenc} % set input encoding to utf8

\usepackage{tcolorbox}

%%CHANGEMENT DE LANGUE
\usepackage[french]{babel} %change la langue pour le français

%%% PAGE DIMENSIONS
% Set up the paper to be as close as possible to both A4 & letter:
\settrimmedsize{297mm}{210mm}{*} % letter = 11in tall; a4 = 210mm wide modifié A4 pur
\setlength{\trimtop}{0pt}
\setlength{\trimedge}{\stockwidth}
\addtolength{\trimedge}{-\paperwidth}
\setlxvchars \setxlvchars[\normalfont]%censé régler la largeur du bloc de caractères pour un nbre optimum de lettres /lignes en fonction de la police
\settypeblocksize{*}{\lxvchars}{1.618} % we want to the text block to have golden proportionals
\setbinding{20pt} %ajoute un espace pour la reliure (cette valeur est soustraite de la largeur théorique de la page)
\setulmargins{50pt}{*}{*} % 50pt upper margins
\setlrmargins{*}{*}{1.618} % golden ratio again for left/right margins
\setheaderspaces{*}{*}{1.618}

\checkandfixthelayout 
% This is from memman.pdf



%%% COULEURS & LOGO
\definecolor{logo_purple}{RGB}{109,20,71}%Définit la couleur POURPRE du logo SPMS
\definecolor{logo_blue}{RGB}{15,115,159}%Définit la couleur BLEUE du logo SPMS
\definecolor{logo_green}{RGB}{129,166,42}%Définit la couleur VERTE du logo SPMS
\definecolor{logo_orange}{RGB}{238,128,15}%Définit la couleur ORANGE du logo SPMS

\newcommand{\logoColor}{\color{logo_blue}}

\newcounter{logoUn}\setcounter{logoUn}{0}
\newcounter{logoDeux}\setcounter{logoDeux}{1}
\newcounter{logoTrois}\setcounter{logoTrois}{2}
\newcounter{logoQuatre}\setcounter{logoQuatre}{3}
\renewcommand{\memendofchapterhook}{%%redéfinition d'une commande (vide) de memoir qui s'exécute *après* la commande \chapter 
    \stepcounter{logoUn}
    \stepcounter{logoDeux}
    \stepcounter{logoTrois}
    \stepcounter{logoQuatre}
    \ifnum \thelogoUn=4\relax {cas1 \gdef\logoColor{\color{logo_blue}}\pagestyle{my_page_style}} \fi
    \ifnum \thelogoDeux=4\relax {cas2 \gdef\logoColor{\color{logo_green}}\pagestyle{my_page_style}} \fi
    \ifnum \thelogoTrois = 4\relax {cas3 \gdef\logoColor{\color{logo_orange}}\pagestyle{my_page_style}} \fi
    \ifnum \thelogoQuatre = 4\relax {cas4} \fi
    %   \else {cas autre}
    %\fi     

} 

%%% HEADERS & FOOTERS

\copypagestyle{my_page_style}{ruled} %définit un style local modifiable basé sur "ruled"
    \makeheadfootruleprefix{my_page_style}{\logoColor}{} %définit la couleur du trait entre l'en-tête et le texte et entre le texte et le pied de page (N/A pour le style "ruled")
    \makeheadrule{my_page_style}{\textwidth}{4pt} %style de page, longueur de la règle, épaisseur (\normalrulethickness = 0.4pt)





%applique ce style au document
\pagestyle{my_page_style} % try also: empty , plain , headings , ruled , Ruled , companion 


%%% CHAPTERS
\chapterstyle{bianchi} % try also: default , section , hangnum , companion , article, demo, verville, wilsondob, ger, komalike,dash, culver, crosshead, chappell, brotherton,bringhurst, 

%% END Memoir customization



%%% BEGIN DOCUMENT
\begin{document}


\mainmatter %correspond au contenu principal, les pages sont numérotées en chiffres arabes, la numérotation reprend au début. 
        %Attention, les appendices font partie du contenu principal!


\chapter{test}\thispagestyle{my_page_style}
\thelogoUn
\section{test}
\subsection{test}
 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vulputate mauris at nisl tristique, eu pharetra turpis suscipit. Suspendisse sed pellentesque ligula. Maecenas pulvinar mi velit, at lobortis velit faucibus sed. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Ut rhoncus eros maximus nibh semper varius. Proin mollis risus augue, sit amet porta metus aliquet et. Cras sit amet scelerisque lorem. Vivamus rhoncus tincidunt lacus, non malesuada enim sollicitudin non. Sed consectetur ipsum eget bibendum vehicula. Phasellus in laoreet purus, accumsan convallis ex. Ut sed semper ipsum. Nam vitae eros a tortor iaculis varius. Ut eu sem sed arcu venenatis pulvinar. Curabitur augue arcu, tempus in suscipit sit amet, auctor a nisi. In laoreet ornare lacus, in iaculis odio tincidunt ac.

 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vulputate mauris at nisl tristique, eu pharetra turpis suscipit. Suspendisse sed pellentesque ligula. Maecenas pulvinar mi velit, at lobortis velit faucibus sed. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Ut rhoncus eros maximus nibh semper varius. Proin mollis risus augue, sit amet porta metus aliquet et. Cras sit amet scelerisque lorem. Vivamus rhoncus tincidunt lacus, non malesuada enim sollicitudin non. Sed consectetur ipsum eget bibendum vehicula. Phasellus in laoreet purus, accumsan convallis ex. Ut sed semper ipsum. Nam vitae eros a tortor iaculis varius. Ut eu sem sed arcu venenatis pulvinar. Curabitur augue arcu, tempus in suscipit sit amet, auctor a nisi. In laoreet ornare lacus, in iaculis odio tincidunt ac.
 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vulputate mauris at nisl tristique, eu pharetra turpis suscipit. Suspendisse sed pellentesque ligula. Maecenas pulvinar mi velit, at lobortis velit faucibus sed. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Ut rhoncus eros maximus nibh semper varius. Proin mollis risus augue, sit amet porta metus aliquet et. Cras sit amet scelerisque lorem. Vivamus rhoncus tincidunt lacus, non malesuada enim sollicitudin non. Sed consectetur ipsum eget bibendum vehicula. Phasellus in laoreet purus, accumsan convallis ex. Ut sed semper ipsum. Nam vitae eros a tortor iaculis varius. Ut eu sem sed arcu venenatis pulvinar. Curabitur augue arcu, tempus in suscipit sit amet, auctor a nisi. In laoreet ornare lacus, in iaculis odio tincidunt ac.

\par


\chapter{Description}
Valeur : \thelogoUn

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vulputate mauris at nisl tristique, eu pharetra turpis suscipit. Suspendisse sed pellentesque ligula. Maecenas pulvinar mi velit, at lobortis velit faucibus sed. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Ut rhoncus eros maximus nibh semper varius. Proin mollis risus augue, sit amet porta metus aliquet et. Cras sit amet scelerisque lorem. Vivamus rhoncus tincidunt lacus, non malesuada enim sollicitudin non. Sed consectetur ipsum eget bibendum vehicula. Phasellus in laoreet purus, accumsan convallis ex. Ut sed semper ipsum. Nam vitae eros a tortor iaculis varius. Ut eu sem sed arcu venenatis pulvinar. Curabitur augue arcu, tempus in suscipit sit amet, auctor a nisi. In laoreet ornare lacus, in iaculis odio tincidunt ac.

Donec faucibus, elit non sagittis feugiat, justo erat luctus eros, nec hendrerit nulla mi non nisi. Phasellus bibendum, libero a suscipit efficitur, diam ligula congue nunc, feugiat finibus eros urna nec nisi. Vestibulum arcu mi, tempor ac lorem in, eleifend facilisis lacus. Mauris ut velit at leo pellentesque placerat. Donec ut ipsum a elit elementum viverra. Phasellus blandit, tortor et tincidunt congue, nibh ipsum convallis elit, ac rutrum dui odio in quam. Morbi eu lorem sed erat dignissim lacinia. Aliquam rhoncus et tellus et dictum. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Nam ullamcorper dapibus dignissim. Nullam laoreet semper ipsum, ut dapibus ligula vehicula a. Ut nisi massa, iaculis a magna mattis, malesuada facilisis elit. Ut vestibulum dolor ac interdum blandit. Interdum et malesuada fames ac ante ipsum primis in faucibus. Suspendisse porta augue risus, ut tristique dolor commodo lobortis. Donec sagittis at diam quis sodales. Praesent maximus sollicitudin felis, et fermentum massa pretium non.

\chapter{Dummy chapter}
\section{Dummy section}
Nam ullamcorper dapibus dignissim. Nullam laoreet semper ipsum, ut dapibus ligula vehicula a. Ut nisi massa, iaculis a magna mattis, malesuada facilisis elit. Ut vestibulum dolor ac interdum blandit. Interdum et malesuada fames ac ante ipsum primis in faucibus. Suspendisse porta augue risus, ut tristique dolor commodo lobortis. Donec sagittis at diam quis sodales. Praesent maximus sollicitudin felis, et fermentum massa pretium non. 

\clearpage
Finis!

\end{document}

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

相关内容