1

I am trying to have an article class document with this requirements:

  • Twoside
  • Two columns with multicol except section and subsection title
  • Header, footer and section title colorized according to the section number

I did it but infortunatly the bottom margins have a weird behaviour. The margin changes on even or odd page. I have absolutely no clue how to solve this… Here is my code with the problem.

\documentclass[12pt,twoside,english]{article}

\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{ifthen}
\usepackage{multicol}
\usepackage{lipsum}
\usepackage[framemethod=TikZ]{mdframed}

\usepackage{tikz}
\usepackage{xcolor}
%\usepackage{libertine}
%\usepackage[bottom]{footmisc}
\usepackage{geometry}
\geometry{verbose,
top=1cm,
bottom=1.8cm,
left=2.2cm,
right=2.2cm}

\usepackage{setspace}
%%Define a color for each section
\definecolor{zz0}{rgb}{.1,.8,.3}
\definecolor{zz1}{rgb}{.8,.2,.2}
\definecolor{zz2}{rgb}{.7,.9,.1}
\definecolor{zz3}{rgb}{.6,.4, 0}
\definecolor{zz4}{rgb}{.5,.1,.9}
\definecolor{zz5}{rgb}{.1, 0,.8}
\definecolor{zz6}{rgb}{.7,.2,.6}
\definecolor{zz7}{rgb}{.9,.3,.8}
\definecolor{zz8}{rgb}{0 ,.4,.1}
\definecolor{zz9}{rgb}{.7,.5,.2}
\definecolor{zz10}{rgb}{.1,.6,.3}
\definecolor{zz11}{rgb}{.6,.8,.5}

\renewcommand*{\hrulefill}[2][0pt]{\leavevmode \leaders \hbox to 1pt{\rule[#1]{1pt}{#2}} \hfill \kern 0pt} 

% For headers
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}%
\renewcommand{\headrulewidth}{1pt}% 2pt header rule
\renewcommand{\headrule}{\hbox to\headwidth{%
    \color{zz\arabic{section}}\leaders\hrule height \headrulewidth\hfill}}
\fancyhead[LE,RO]{\raisebox{-.9\baselineskip}[0pt][0pt]{\textsc{\textsf{\leftmark}}}}
\fancyfoot[RO]{ \color{zz\arabic{section}} \hrulefill{6pt} \normalcolor \,
some random text %
\color{zz\arabic{section}} \rule{10pt}{6pt} \normalcolor \thepage}%
\fancyfoot[LE]{\thepage \, \color{zz\arabic{section}} \rule{10pt}{6pt} \normalcolor%
Some random text %
\color{zz\arabic{section}} \hrulefill{6pt}}


\renewcommand{\sectionmark}[1]{\markboth{\color{zz\arabic{section}}#1}{}}
%% Create if environment
\makeatletter
\def\ifenv#1{
   \def\@tempa{#1}%
   \ifx\@tempa\@currenvir
      \expandafter\@firstoftwo
    \else
      \expandafter\@secondoftwo
   \fi
}
\makeatother

%% Redefine the section
\usepackage{titlesec}

\let\oldsection\section
\renewcommand{\section}[1]{%
    \ifenv{multicols}{\end{multicols}\clearpage}{\clearpage}%
    \begin{multicols}{2}[\oldsection{#1}]}
%% Redefine the end of the document
\let\oldend\enddocument
\renewcommand*{\enddocument}{\ifenv{multicols}{\end{multicols}}{}\oldend}
%% Redefine the subsection
\let\oldsubsection\subsection
\renewcommand{\subsection}[1]{%
   \ifenv{multicols}{\end{multicols}}{}%
   \begin{multicols}{2}[\oldsubsection{#1}]}

\titleformat{\section}{\LARGE\normalfont\color{zz\arabic{section}}}{\thesection}{1em}{}
\titleformat{\subsection}{\Large\normalfont\color{zz\arabic{section}}}{\thesubsection}{1em}{}


\begin{document}

\section{Section}
\lipsum[1-2]
\subsection{Subsection}
\blindtext[4]

\section{Section}
\blindtext[2]
\subsection{Subsection}
\blindtext[2]

\section{Section}
\blindtext[2]
\subsection{Subsection}
\blindtext[2]

\section{Section}
\subsection{Subsection}
\blindtext[2]

\section{Section}
\subsection{Subsection}
\blindtext[2]


\end{document}

Thanks for your help

1 Answer 1

1

The problem arises from the use of fancyhdr. I don't know what exactly went wrong with it, but it fails to set the footer on a consistent height. Therefore I recreated your overall look using scrlayer-scrpage. (I hope I got everything right)

I made some other changes, mostly on code formatting. I also removed the redefinition of \enddocument and used \AtEndDocument instead. Also I used \LetLtxMacro instead of \let for \section and \subsection. This will be important if you use optional arguments for the redefinition of \section or \subsection in the future.

Code:

\documentclass[12pt,twoside,english]{article}

\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{multicol}
\usepackage{lipsum}

\usepackage{xcolor}
\usepackage{libertine}
\usepackage{geometry}
\geometry
  {%
    verbose
    ,top=1cm
    ,bottom=1.8cm
    ,left=2.2cm
    ,right=2.2cm
    ,headsep=4mm % control the height of the header (the bigger the higher)
    ,headheight=15pt
  }

%%Define a color for each section
\definecolor{zz0}{rgb}{.1,.8,.3}
\definecolor{zz1}{rgb}{.8,.2,.2}
\definecolor{zz2}{rgb}{.7,.9,.1}
\definecolor{zz3}{rgb}{.6,.4, 0}
\definecolor{zz4}{rgb}{.5,.1,.9}
\definecolor{zz5}{rgb}{.1, 0,.8}
\definecolor{zz6}{rgb}{.7,.2,.6}
\definecolor{zz7}{rgb}{.9,.3,.8}
\definecolor{zz8}{rgb}{0 ,.4,.1}
\definecolor{zz9}{rgb}{.7,.5,.2}
\definecolor{zz10}{rgb}{.1,.6,.3}
\definecolor{zz11}{rgb}{.6,.8,.5}

\renewcommand*\hrulefill[2][0pt]
  {%
    \leavevmode \leaders \hbox to 1pt{\rule[#1]{1pt}{#2}}\hfill \kern 0pt
  }

\usepackage{scrlayer-scrpage}
\clearpairofpagestyles
\ohead{\raisebox{-.2\baselineskip}[0pt][0pt]{\leftmark}}
\rofoot
  {%
    \color{zz\arabic{section}}%
    \hrulefill{6pt}%
    \normalcolor
    \, Some random text
    \color{zz\arabic{section}}%
    \rule{10pt}{6pt}
    \normalcolor
    \pagemark
  }
\lefoot
  {%
    \pagemark
    \,
    \color{zz\arabic{section}}%
    \rule{10pt}{6pt}%
    \normalcolor
    \ Some random text
    \color{zz\arabic{section}}%
    \hrulefill{6pt}%
  }
% For headers
\KOMAoption{headsepline}{1pt} % rule thickness
\setkomafont{headsepline}{\color{zz\arabic{section}}} % rule colour
\setkomafont{pagehead}{\color{zz\arabic{section}}\normalfont\sffamily\scshape}
\setkomafont{pagefoot}{\normalfont}

\renewcommand\sectionmark[1]
  {%
    \markboth{\color{zz\arabic{section}}#1}{}%
  }
%% Create if environment
\makeatletter
\def\ifenv#1%
  {%
    \def\@tempa{#1}%
    \ifx\@tempa\@currenvir
      \expandafter\@firstoftwo
    \else
      \expandafter\@secondoftwo
    \fi
  }
\makeatother

%% Redefine the section
\usepackage{titlesec}
\usepackage{letltxmacro}
\titleformat{\section}
  {%
    \LARGE\normalfont\color{zz\arabic{section}}%
  }
  {\thesection}{1em}{}
\titleformat{\subsection}
  {%
    \Large\normalfont\color{zz\arabic{section}}%
  }
  {\thesubsection}{1em}{}

%% Redefine the end of the document
\AtEndDocument{\ifenv{multicols}{\end{multicols}}{}}
%% Redefine the section
\LetLtxMacro\oldsection\section
\renewcommand\section[1]
  {%
    \ifenv{multicols}{\end{multicols}}{}%
    \clearpage
    \begin{multicols}{2}[\oldsection{#1}]%
  }
%% Redefine the subsection
\LetLtxMacro\oldsubsection\subsection
\renewcommand\subsection[1]
  {%
    \ifenv{multicols}{\end{multicols}}{}%
    \begin{multicols}{2}[\oldsubsection{#1}]%
  }



\begin{document}

\section{Section}
\lipsum[1-2]
\subsection{Subsection}
\blindtext[4]

\section{Section}
\blindtext[2]
\subsection{Subsection}
\blindtext[2]

\section{Section}
\blindtext[2]
\subsection{Subsection}
\blindtext[2]

\section{Section}
\subsection{Subsection}
\blindtext[2]

\section{Section}
\subsection{Subsection}
\blindtext[2]


\end{document}

enter image description here

1
  • Thank you very much ! I don't understand why fancyhdr does not work. I did not know scrlayer-scrpage package but it seems very usefull ! =) Thanks again for all your advices !
    – AdrienD
    Oct 28, 2018 at 17:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .