# Problem with \renewcommand in LaTeX

\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,
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}


Problem: On compiling with pdflatex, the output PDF contains "image2" only - for both chapter headings.

i.e. The code section:

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

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


is producing "image2" for both the Chapters.

-

This is caused by the asynchronous behaviour of TeX: Your images are placed when pages are built, which happens well after typesetting. If you insert a \newpage in front of \renewcommand\chapterillustration{image2}, the problem goes away.

-
Thanks, it works. But I can't insert blank new pages in the book, after every Chapter. Can't we have some another solution for this? –  Sandeep Singh Aug 25 '12 at 8:27
I haven't studied your layout in detail, but I had assumed that, when you make chapter illustrations part of the page layout, that you do want each chapter to start on a new page? If this is not so, then I am confused as to what you are trying to accomplish. –  Harald Hanche-Olsen Aug 25 '12 at 8:30
Sorry, I misunderstood your point. Your fix is fine. –  Sandeep Singh Aug 25 '12 at 8:37

Maybe a uglier solution but you get rid of the \newpage:

Change the following lines in your preamble

%% 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}


and use it like this in your document:

\chapter{First Chapter}
\chapterillustration{image1}

\chapter{Second Chapter}
\chapterillustration{image2}

-
If you use \roman instead of \alph you won't run out of numbers ;-) –  Stephan Lehmke Aug 25 '12 at 9:42
@StephanLehmke Good point. I wasn't aware that the romans foresightedly designed their numeral system compatible with the name conventions of TeX macros. –  periswon Aug 25 '12 at 10:28
leads to compilation error:File \chillui .mps' not found –  Sandeep Singh Aug 25 '12 at 13:43
Did you take into account that you have to switch the order of \chapter and \chapterillustration in your original code? When I use \chapterillustration before \chapter` I get the same error. –  periswon Aug 25 '12 at 14:43
@periswon:Thanks for your kind help. I want to share this will all of you:stackoverflow.com/questions/491904/…. Setting: \documentclass[oneside]{book} removes ALL the Blank Pages from the Book :) –  Sandeep Singh Aug 25 '12 at 14:58