LaTeX 中的 \renewcommand 问题

LaTeX 中的 \renewcommand 问题

请考虑以下代码:

\documentclass[11pt,twoside,openany]{book}

\usepackage[svgnames,x11names]{xcolor}
\usepackage{wallpaper}
\usepackage{changepage}
\usepackage[explicit]{titlesec}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}
\usepackage{geometry}
\geometry{
   paperwidth=216mm, paperheight=303mm,
   left=23mm,  %% or inner=23mm
   right=18mm, %% or outer=18mm
   top=23mm, bottom=23mm,
   headheight=\baselineskip,
   headsep=7mm,
   footskip=7mm
}


%% Command to hold chapter illustration image
\newcommand\chapterillustration{}

%% Define how the chapter title is printed
\titleformat{\chapter}{}{}{0pt}{
%% Background image at top of page
\ThisULCornerWallPaper{1}{\chapterillustration}
%% Draw a semi-transparent rectangle across the top
\tikz[overlay,remember picture]
  \fill[LightSalmon1,opacity=.7]
  (current page.north west) rectangle 
  ([yshift=-3cm] current page.north east);
  %% Check if on an odd or even page
  \checkoddpage\strictpagecheck
  %% On odd pages, "logo" image at lower right
  %% corner; Chapter number printed near spine
  %% edge (near the left); chapter title printed
  %% near outer edge (near the right).
  \ifoddpage{
    \ThisLRCornerWallPaper{.35}{image3}
    \begin{tikzpicture}[overlay,remember picture]
    \node[anchor=south west,
      xshift=20mm,yshift=-30mm,
      font=\sffamily\bfseries\huge] 
      at (current page.north west) 
      {\chaptername\ \thechapter};
    \node[fill=Sienna!80!black,text=white,
      font=\Huge\bfseries, 
      inner ysep=12pt, inner xsep=20pt,
      rounded rectangle,anchor=east, 
      xshift=-20mm,yshift=-30mm] 
      at (current page.north east) {#1};
    \end{tikzpicture}
  }
  %% On even pages, "logo" image at lower left
  %% corner; Chapter number printed near outer
  %% edge (near the right); chapter title printed
  %% near spine edge (near the left).
  \else {
    \ThisLLCornerWallPaper{.35}{image3}
    \begin{tikzpicture}[overlay,remember picture]
    \node[anchor=south east,
      xshift=-20mm,yshift=-30mm,
      font=\sffamily\bfseries\huge] 
      at (current page.north east)
      {\chaptername\ \thechapter};
    \node[fill=Sienna!80!black,text=white,
      font=\Huge\bfseries,
      inner sep=12pt, inner xsep=20pt,
      rounded rectangle,anchor=west,
      xshift=20mm,yshift=-30mm] 
      at ( current page.north west) {#1};
    \end{tikzpicture}
  }
  \fi
}
\titlespacing*{\chapter}{0pt}{0pt}{135mm}


\begin{document}
\renewcommand\chapterillustration{image1}
\chapter{First Chapter}

\renewcommand\chapterillustration{image2}
\chapter{Second Chapter}

\end{document}

问题:使用 pdflatex 进行编译时,输出 PDF 仅包含“image2”——对于两个章节标题。

即代码部分:

\renewcommand\chapterillustration{image1}
\chapter{First Chapter}

\renewcommand\chapterillustration{image2}
\chapter{Second Chapter}

正在为两个章节制作“image2”。

请帮助我解决这个问题。

答案1

这是由 TeX 的异步行为引起的:您的图像是在构建页面时放置的,而构建页面是在排版之后发生的。如果您\newpage在 前面插入\renewcommand\chapterillustration{image2},问题就会消失。

答案2

也许是一个更丑陋的解决方案,但你摆脱了\newpage

更改序言中的以下几行

%% Command to hold chapter illustration image
\newcommand{\chapterillustration}[1]{%
\expandafter\newcommand\csname chillu\roman{chapter}\endcsname{#1}}

%% Define how the chapter title is printed
\titleformat{\chapter}{}{}{0pt}{
%% Background image at top of page
\ThisULCornerWallPaper{1}{\csname chillu\roman{chapter}\endcsname}

并在您的文档中像这样使用它:

\chapter{First Chapter}
\chapterillustration{image1}

\chapter{Second Chapter}
\chapterillustration{image2}

相关内容