根据章节号有条件打印

根据章节号有条件打印

我已经制作了自定义章节标题样式,并且我想将其与目录一起使用。

因此为此我有:

\documentclass{memoir}
\usepackage[utf8]{inputenc}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usepackage{arrayjob}

\title{demo}


\newarray\dnaletters
\readarray{dnaletters}{A&C&G&T}
\colorlet{Random1}{cyan}
\colorlet{Random2}{yellow}
\colorlet{Random3}{red}
\colorlet{Random4}{green}

\makeatletter
    \makechapterstyle{thesisStyle}{
        \renewcommand{\chapternamenum}{}
        \renewcommand{\chapnumfont}{\normalfont\Huge\bfseries}
        \renewcommand{\printchapternum}{}
        \renewcommand{\printchaptername}{}
        \renewcommand\printchaptertitle[1]{
            \begin{tikzpicture}[remember picture,overlay,shift={(current page.north west)},yshift=-3cm]
                \path[fill=Black] (0,0) rectangle(\paperwidth,3cm);
                \foreach \x in {0,1, ..., 70}{
                    \foreach \y in {0, ...,7} 
                        \pgfmathsetmacro\Random{random(1,4)}
                    \node[draw=none,color=Random\Random,anchor=south west,font=\tiny,xshift=-.05cm] at (\x*.3cm,\y*.33cm) {\dnaletters(\Random)};
                };
                \node[anchor=east,xshift=.9\paperwidth,rectangle, rounded corners=20pt,inner sep=11pt, fill=MidnightBlue] {
                    \ifnum\value{chapter}>\value{0}
                        \normalfont\Huge\bfseries\color{white}\thechapter|~##1
                    \else
                        \normalfont\Huge\bfseries\color{white}##1
                    \fi
                };
            \end{tikzpicture}
        }
        \renewcommand{\afterchapternum}{}
    }
\makeatother

\headstyles{thesisStyle}
\chapterstyle{thesisStyle}

\begin{document}

\tableofcontents
\chapter{test}

\end{document}

我不明白引发的错误:

Missing number, treated as zero.

<to be read again> 
                   \c@0 
l.50 \chapter{test}

A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)

结果可见于:https://fr.sharelatex.com/project/576e90018ad9fc69396c79eb

感谢您的帮助

答案1

这是由 引起的\value{0},这是无效的。简单0就足够了。\value仅在访问计数器时使用,就像使用 一样\value{chapter}

另外,你不需要在这里使用\makeatletter/ \makeatother。请参阅做什么\makeatletter\makeatother做什么?,尽管我怀疑你已经知道这一点。

输出

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

代码

\documentclass{memoir}
\usepackage[utf8]{inputenc}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usepackage{arrayjob}

\title{demo}


\newarray\dnaletters
\readarray{dnaletters}{A&C&G&T}
\colorlet{Random1}{cyan}
\colorlet{Random2}{yellow}
\colorlet{Random3}{red}
\colorlet{Random4}{green}

%\makeatletter % not needed here. You are not accessing commands with @ in them.
    \makechapterstyle{thesisStyle}{
        \renewcommand{\chapternamenum}{}
        \renewcommand{\chapnumfont}{\normalfont\Huge\bfseries}
        \renewcommand{\printchapternum}{}
        \renewcommand{\printchaptername}{}
        \renewcommand\printchaptertitle[1]{
            \begin{tikzpicture}[remember picture,overlay,shift={(current page.north west)},yshift=-3cm]
                \path[fill=Black] (0,0) rectangle(\paperwidth,3cm);
                \foreach \x in {0,1, ..., 70}{
                    \foreach \y in {0, ...,7} 
                        \pgfmathsetmacro\Random{random(1,4)}
                    \node[draw=none,color=Random\Random,anchor=south west,font=\tiny,xshift=-.05cm] at (\x*.3cm,\y*.33cm) {\dnaletters(\Random)};
                };
                \node[anchor=east,xshift=.9\paperwidth,rectangle, rounded corners=20pt,inner sep=11pt, fill=MidnightBlue] {
                    \ifnum\value{chapter}>0% \value{0}, use \value only for accessing the value of a counter.
                        \normalfont\Huge\bfseries\color{white}\thechapter|~##1
                    \else
                        \normalfont\Huge\bfseries\color{white}##1
                    \fi
                };
            \end{tikzpicture}
        }
        \renewcommand{\afterchapternum}{}
    }
%\makeatother not needed either

\headstyles{thesisStyle}
\chapterstyle{thesisStyle}

\begin{document}

\tableofcontents
\chapter{test}
\end{document}

相关内容