# Code improvement and suggestions in template

Consider the following MWE:

\documentclass[12pt,twoside,letterpaper]{report}

\usepackage[text={4.95in,7.5in},centering,bottom=1.5in,showframe]{geometry}
\usepackage[dvipsnames]{xcolor}
\usepackage{calc}
\usepackage[explicit]{titlesec}
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage[demo]{graphicx}
\usepackage{tikz}
\usepackage{amsmath,amssymb}
\usepackage{tikzpagenodes}

\usetikzlibrary{calc,positioning}

\setlength{\oddsidemargin}{-0.25in}
\setlength{\evensidemargin}{1.75in}

\DeclareFixedFont{\chapterfont}{T1}{ppl}{m}{n}{0.365in}
\DeclareFixedFont{\chapternumfont}{T1}{ppl}{m}{n}{1in}
%-------------------------------------------------------------------------
%                             Chapter Style
%-------------------------------------------------------------------------
\titleformat{\chapter}[display]
{\chapterfont}
{
\ifodd\thepage
\begin{tikzpicture}[overlay, remember picture]
\node[minimum width=2.5in, text width=\marginparwidth+\evensidemargin-2\marginparsep+\oddsidemargin, minimum height=10pt, rectangle, fill=cyan, outer sep=0pt, anchor=north east, align=right] at ($(current page.north east)$) {};
\node[text=cyan](chapnum) at ($(current page.north east)+(-2\marginparwidth+2.85\marginparsep,-1.125in)$) {\chapternumfont\thechapter};
\node[cyan,outer sep=0pt,left=0em of chapnum] (vline) {$\left|\vphantom{\text{\chapternumfont 1}}\right.$};
\end{tikzpicture}%
\else
\begin{tikzpicture}[overlay, remember picture]
\node[minimum width=2.5in, text width=\marginparwidth+\evensidemargin-2\marginparsep+\oddsidemargin, minimum height=10pt, rectangle, fill=magenta, outer sep=0pt, anchor=north west, align=right] at ($(current page.north west)$) {};
\node[text=magenta](chapnum) at ($(current page.north west)+(2\marginparwidth-2.85\marginparsep,-1.125in)$) {\chapternumfont\thechapter};
\node[magenta,outer sep=0pt,right=0em of chapnum,at=(chapnum.east)] (vline) {$\left|\vphantom{\text{\chapternumfont 1}}\right.$};
\end{tikzpicture}%
\fi
}
{-2.125in}
{\parbox{0.85\textwidth}{#1}}
[\vspace*{.35in}]
%-------------------------------------------------------------------------
%-------------------------------------------------------------------------
\fancypagestyle{plain}{%
\fancyhf{} % clear all header and footer fields
\fancyfoot{} % clear all footer fields
\fancyfoot[RO]{\thepage$|$}
\fancyfoot[LE]{$|$\thepage}
\renewcommand{\footrulewidth}{0pt}
}
%-------------------------------------------------------------------------
%-------------------------------------------------------------------------
\fancyfoot{} % clear all footer fields
\fancyfoot[RO]{\thepage$|$}
\fancyfoot[LE]{$|$\thepage}
\renewcommand{\footrulewidth}{0pt}

\pagestyle{fancy}

\begin{document}
\chapter{L'Hopitals Rule}
\ifodd\thepage
\begin{tikzpicture}[remember picture, overlay, outer sep=0pt]
\node[anchor=north west] at (current page marginpar area.north west) {\includegraphics[height=\textheight]{figure}};
\end{tikzpicture}
\else
\begin{tikzpicture}[remember picture, overlay, outer sep=0pt]
\node[anchor=north east] at (current page marginpar area.north east) {\includegraphics[height=\textheight]{figure}};
\end{tikzpicture}
\fi
test\footnote{test}
\lipsum[1-2]
\lipsum
\chapter{Descartes Rule of Signs}
\ifodd\thepage
\begin{tikzpicture}[remember picture, overlay, outer sep=0pt]
\node[anchor=north west] at (current page marginpar area.north west) {\includegraphics[height=\textheight]{figure}};
\end{tikzpicture}
\else
\begin{tikzpicture}[remember picture, overlay, outer sep=0pt]
\node[anchor=north east] at (current page marginpar area.north east) {\includegraphics[height=\textheight]{figure}};
\end{tikzpicture}
\fi
\lipsum[1-2]
\chapter{Mean Value Theorem and Intermediate Value Theorem}
\lipsum[1-2]
\end{document}


This yields:

As you can see there are several optimization problems I am facing:

1. The chapter style may not be properly defined.
2. The vertical line beside the chapter numbering is not properly aligned with the horizontal bar above it.
3. The chapter names like L'Hopitals Rule are not properly vertically centered with the vertical bar. This can be obvious in the third image.
4. There is a vertical displacement of where the text starts after the chapter name which is clearly obvious in the third image.
5. The geometry of the page is not properly defined. I just want a regular lettersize paper with about the same output as the image shows.

Things I am considering defining for this template are:

1. To be able to place the horizontal bar in every page that belongs to the chapter. Possibly a vertical bar to the right on odd pages starting at current page.north east and going down possible an inch.
2. To be able to define different colors for every chapter.
3. To be able to place different images and different pages by using a macro possibly in the preamble. Something like: \pagepic{<pagenum>}{pic}. This needs to be positioned as shown. Note the images will use the space indicated in the demo. One pic per page.

Any suggestions will be highly appreciated. I am doing this just for leisure. Just trying to learn new tricks and optimization techniques.

• Just to point out that when your chapter number goes into double figures, the alignment goes way off. May 8 '13 at 7:21
• I'm looking at your code but finding it quite hard to understand, partly because there are lots of magic numbers everywhere -- 2.85\marginparsep, -1.125in, etc. Could you add more comments to explain where these come from? (If they're just invented by trial-and-error it's no surprise that the various objects don't quite line up!) May 8 '13 at 7:24
• @JohnWickerson -2.85\marginparsep is just a forced dimension that I used to make it seem to be aligned. -1.125in is just a particular height from the top most of the page. It has no special significance. May 8 '13 at 15:28

Your code is not optimized. I also didn't have much time to go further. But I did some changes. See if it looks better.

\documentclass[12pt,twoside,letterpaper]{report}

\usepackage[text={4.95in,7.5in},centering,bottom=1.5in,showframe]{geometry}
\usepackage[dvipsnames]{xcolor}
\usepackage{calc}
\usepackage[explicit]{titlesec}
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage[demo]{graphicx}
\usepackage{tikz}
\usepackage{amsmath,amssymb}
\usepackage{tikzpagenodes}

\usepackage{anyfontsize}

\usetikzlibrary{calc,positioning}

\setlength{\oddsidemargin}{-0.25in}
\setlength{\evensidemargin}{1.75in}

\newcommand{\chapterfont}{%
\fontencoding{T1}
\fontfamily{ppl}
\fontseries{m}
\fontshape{n}
\fontsize{26}{28}\selectfont}

\newcommand{\chapternumfont}{%
\fontencoding{T1}
\fontfamily{ppl}
\fontseries{m}
\fontshape{n}
\fontsize{60}{60}\selectfont}

\newcommand{\chapterpicture}{%
\ifodd\thepage
\begin{tikzpicture}[remember picture, overlay, outer sep=0pt,inner sep=0pt]
\node[anchor=north west] at (current page marginpar area.north west) {\includegraphics[height=\textheight,width=\marginparwidth,keepaspectratio]{figure}};
\end{tikzpicture}
\else
\begin{tikzpicture}[remember picture, overlay, outer sep=0pt,inner sep=0pt]
\node[anchor=north east] at (current page marginpar area.north east) {\includegraphics[height=\textheight,width=\marginparwidth,keepaspectratio]{figure}};
\end{tikzpicture}
\fi}

%-------------------------------------------------------------------------
%                             Chapter Style
%-------------------------------------------------------------------------
\titleformat{\chapter}[display]
{\chapterfont}
{
\ifodd\thepage
\begin{tikzpicture}[overlay, remember picture]
\coordinate (aux1) at ($(current page.north east)-(0,10pt)$);
\coordinate (aux2) at ($(current page text area.east)+(\marginparsep,0)$);
\coordinate (aux3) at (aux2|-aux1);
\coordinate (aux4) at ($(current page marginpar area.north west)!0.5!(current page marginpar area.north east)$);
\path[fill=cyan] (aux3) rectangle (current page.north east) node [midway] (barabove) {};
\node[text=cyan,font=\chapternumfont,yshift=3cm] at (aux4) (chapnum) {\thechapter};
\node[cyan,outer sep=0pt,inner sep=0pt] (vline) at (aux3|-chapnum) {$\left|\vphantom{\text{\chapternumfont 1}}\right.$};
\end{tikzpicture}%
\else
\begin{tikzpicture}[overlay, remember picture]
\coordinate (aux1) at ($(current page.north west)-(0,10pt)$);
\coordinate (aux2) at ($(current page text area.west)-(\marginparsep,0)$);
\coordinate (aux3) at (current page.north west-|aux2);
\coordinate (aux4) at ($(current page marginpar area.north west)!0.5!(current page marginpar area.north east)$);
\path[fill=magenta] (aux1) rectangle (aux3) node [midway] (barabove) {};
\node[text=magenta,font=\chapternumfont,yshift=3cm] at (aux4) (chapnum) {\thechapter};
\node[magenta,outer sep=0pt,inner sep=0pt] (vline) at (aux3|-chapnum) {$\left|\vphantom{\text{\chapternumfont 1}}\right.$};
\end{tikzpicture}%
\fi
}
{-2.125in}
{
\begin{tikzpicture}[overlay, remember picture]
\node [align=left,text width=\textwidth,anchor=west] at (current page text area.west|-chapnum) {#1};
\end{tikzpicture}
}
[\vspace*{.35in}]
%-------------------------------------------------------------------------
%-------------------------------------------------------------------------
\fancypagestyle{plain}{%
\fancyhf{} % clear all header and footer fields
\fancyfoot{} % clear all footer fields
\fancyfoot[RO]{\thepage$|$}
\fancyfoot[LE]{$|$\thepage}
\renewcommand{\footrulewidth}{0pt}
}
%-------------------------------------------------------------------------
%-------------------------------------------------------------------------
\fancyfoot{} % clear all footer fields
\fancyfoot[RO]{\thepage$|$}
\fancyfoot[LE]{$|$\thepage}
\renewcommand{\footrulewidth}{0pt}

\pagestyle{fancy}

\begin{document}
\chapter{L'Hopitals Rule}
test\footnote{test}\chapterpicture
\lipsum[1-2]

\chapter{Descartes Rule of Signs}\chapterpicture
\lipsum[1-2]

\chapter{Mean Value Theorem and\\ Intermediate Value Theorem}
\lipsum[1-2]

\setcounter{chapter}{10}
\chapter{Other test}\chapterpicture
\lipsum[1-2]

\setcounter{chapter}{100}
\chapter{Other test}\chapterpicture
\lipsum[1-2]

\end{document}

• For better, I would consider to place an invisible node lets say rectangle to draw the number elements and another to draw the title, and use its anchors to place the other elements. May 8 '13 at 12:45
• There should be a picture in every page. These pictures will be different. It is not only for the chapter pages. May 8 '13 at 15:26