3

I have implemented a custom subsection like this:

\renewcommand\thesubsection{Question \arabic{section}.\arabic{subsection}}

And I would like to clear the page before each subsection so I added this

\usepackage{titlesec}
\newcommand{\subsectionbreak}{\clearpage}

The problem is that I also would like to show the subsection in the right header by doing this:

\pagestyle{fancy}
\rhead{\thesubsection}

Problem: Subsection in header is 1.2 when subsection on page is 1.1

I also tried

\let\oldsection\section
\renewcommand\section{\clearpage\oldsection}

But that just threw me an error...

EDIT: This is the full code of an example that shows the error:

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}


\usepackage{titlesec}
\newcommand{\subsectionbreak}{\clearpage}


\renewcommand\thesection{Section \arabic{section}}
\renewcommand\thesubsection{Question \arabic{section}.\arabic{subsection}}


\pagestyle{fancy}
\rhead{\thesubsection}
\lhead{}


\begin{document}


\section{}

\subsection{}
Subsec 1
\subsection{}
Subsec 2

\end{document}
3
  • Welcome to TeX.SX! Fragments of code aren't really useful. Please post the minimal document that shows the error! – user31729 Dec 15 '15 at 11:51
  • 2
    Ok now I updated it with a minimum not working example – vikeri Dec 15 '15 at 12:00
  • Using a redefinition of \thesubsection will screw up the ToC – user31729 Dec 15 '15 at 12:04
3

I strongly suggest to use a proper question/answer package instead of using \subsection this way, but here's a way to do it anyway.

I've additionally increased the number width spacing in the toc, otherwise the Question 1.2. stuff would be overprinting the dots in a possible usage of the ToC.

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}
\usepackage{etoolbox}
\usepackage{tocloft}

\renewcommand\thesection{Section \arabic{section}}
\renewcommand\thesubsection{Question \arabic{section}.\arabic{subsection}}


\addtolength{\cftsubsecnumwidth}{30pt}% Increase the 'number width'

\pretocmd{\subsection}{\clearpage}{}{} % Prepend `\subsection` with `\clearpage from now on...
\pagestyle{fancy}
\rhead{\ifnum\value{subsection} > 0\thesubsection\fi}
\lhead{}

\begin{document}
\tableofcontents

\clearpage
\section{}

\subsection{}
Subsec 1
\subsection{}
Subsec 2

\end{document}

enter image description here

Update

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}
\usepackage{tocloft}

\renewcommand\thesection{Section \arabic{section}}
\renewcommand\thesubsection{Question \arabic{section}.\arabic{subsection}}


\addtolength{\cftsubsecnumwidth}{30pt}% Increase the 'number width'

\makeatletter
\let\orig@subsection\subsection

\renewcommand{\subsection}{%
  \ifnum\value{subsection} > 0 %
  \clearpage%
  \fi%
  \orig@subsection%
}
\makeatother

\pagestyle{fancy}
\rhead{\ifnum\value{subsection} > 0\thesubsection\fi}
\lhead{}

\begin{document}
\tableofcontents

\clearpage
\section{}

\subsection{}
Subsec 1
\subsection{}
Subsec 2

\end{document}
4
  • Ok great! It works apart from one thing, I do not want the section to have a page break before the first subsection. Could that be fixed? And then I do not understand etoolbox or tocloft? – vikeri Dec 15 '15 at 13:11
  • @vikeri: tocloft is helpful for the ToC appearance, but etoolbox isn't needed really. I can fix – user31729 Dec 15 '15 at 15:14
  • @vikeri: See the update please (at the bottom!) – user31729 Dec 15 '15 at 15:18
  • Great stuff, now it works perfectly! – vikeri Dec 15 '15 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.