我正在尝试根据标题中排版的内容切换标题中的颜色(\rightmark
或\lastrightmark
),但我就是无法让它工作,每次比较都会失败。我首先尝试了\ifthenelse
但我发现它已经过时了,所以我加载了包etoolbox
并尝试了它但\ifdefstring
没有成功。我还看到如果我输入传递给命令的参数,它不是实际的部分名称,但一些命令将围绕它构建,非 ASCII 字符将转换为 LaTeX 内部字符表示。所以我尝试与\typeout
记录的内容进行比较,但仍然没有成功。是否有可能做到这一点?MWE:
\documentclass[twoside,a5paper]{book}
\usepackage[ngerman]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[usenames]{xcolor}
\usepackage{extramarks}
\usepackage{ifthen}
\usepackage{etoolbox}
\usepackage{fancyhdr}
\renewcommand{\sectionmark}[1]{\markright{#1}{}}
\setcounter{secnumdepth}{-1}
\definecolor{testColor}{HTML}{BF4046}
\definecolor{secColor}{HTML}{46BF40}
\newcommand{\currentColor}{testColor}
\newcommand{\getCurrentColor}[1]{%
\ifthenelse{\equal{#1}{Öffentlich}}{\renewcommand{\currentColor}{secColor}}{}%
\ifdefstring{#1}{Öffentlich}{\renewcommand{\currentColor}{secColor}}{}
\ifdefstring{#1}{\foreignlanguage{ngerman}{\bbl@restore@actives\MakeUppercase {\IeC {\"O}ffentlich}}}{\renewcommand{\currentColor}{secColor}}{}
}
\newcommand{\makeHeader}[1]{
\ifthenelse{\equal{#1}{left}}{
\getCurrentColor{\rightmark}%
\colorbox{\currentColor}{\rightmark}
}{
\getCurrentColor{\lastrightmark}%
\colorbox{\currentColor}{\lastrightmark}
}
}
\fancyhead[LE]{\makeHeader{left}}
\fancyhead[RO]{\makeHeader{\right}}
\pagestyle{fancy}
\begin{document}
\section{TestSection}
blabla
\clearpage
\section{Öffentlich}
blabla
\clearpage
\section{TestSection2}
blabla
\end{document}
答案1
这些软件包在标记中添加了很多东西,使得测试更加复杂,但是你可以丢弃所有这些:
\documentclass[twoside,a5paper]{book}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{extramarks}
\usepackage[T1]{fontenc}
\usepackage[usenames]{xcolor}
\usepackage{fancyhdr}
\setlength\headheight{16pt}
\renewcommand{\sectionmark}[1]{\markright{#1}{}}
\setcounter{secnumdepth}{-1}
\definecolor{testColor}{HTML}{BF4046}
\definecolor{secColor}{HTML}{46BF40}
\newcommand{\currentColor}{testColor}
\newcommand{\getCurrentColor}[1]{%
\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter
\expandafter\zz#1\nil% set \z to current section name
\ifx\z\Offentlichtest \renewcommand{\currentColor}{secColor}\fi
\ifx\z\Tonetest \renewcommand{\currentColor}{testColor}\fi
\ifx\z\Ttwotest \renewcommand{\currentColor}{red}\fi
}
\def\zz#1#2#3#4#5\nil{\zzz#4\nil}
\def\zzz#1#2#3#4#5\nil{\def\z{#5}}
\def\Offentlichtest{\IeC {\"O}ffentlich}
\def\Tonetest{TestSection}
\def\Ttwotest{TestSection2}
\newcommand{\makeHeader}[1]{%
\ifx\left#1%
\getCurrentColor{\rightmark}%
\colorbox{\currentColor}{\rightmark}%
\else
\getCurrentColor{\lastrightmark}%
\colorbox{\currentColor}{\lastrightmark}%
\fi
}
\fancyhead[LE]{\makeHeader{\left}}
\fancyhead[RO]{\makeHeader{\right}}
\pagestyle{fancy}
\begin{document}
\section{TestSection}
blabla
\clearpage
\section{Öffentlich}
blabla
\clearpage
\section{TestSection2}
blabla
\end{document}