0

I'm writing a TEXT followed by a PROBLEM SHEET with a set of problems A,B,C and solutions A,B,C. Each problem and solution is in its own section. I have formatted the spacing above and below section titles using the \titlespacing command of the titlesec package, creating two different styles for the TEXT and the PROBLEM SHEET, called \mytextstyle and \myproblemstyle. I also want hyperlinks from problem A to solution A and back, etc., and have thus defined \problemsection and \solutionsection commands that take care of the hyperlinks. However, that sometimes messes up the functionality of \titlespacing. I attach three example outputs below: the desired spacings (left), wrong spacings (middle), and desired spacings but without hyperlinks (right). The middle output was obtained by just commenting out four lines of code that create some TEXT before the PROBLEM SHEET begins -- why this changes the subsequent spacing is a mystery to me.

\documentclass[openany]{book}
\usepackage{titlesec}
\usepackage{color}
\definecolor{darkred}{rgb}{0.7,0,0}
\setlength{\parindent}{0pt}
\pagestyle{empty}

\newcommand\mytextstyle{
\renewcommand{\thesection}{\Roman{section}} 
\titlespacing{\section}{0pt}{2.5\baselineskip}{\baselineskip}
}
\newcommand\myproblemstyle{
\renewcommand{\thesection}{\Alph{section}} 
\titlespacing{\section}{0pt}{1.4\baselineskip}{0.7\baselineskip}
}

\newcommand{\problemsection}[2]{
    {\section[#1]{\hyperref[sec:sol:#2]{#1}}\label{sec:prob:#2}}}
\newcommand{\solutionsection}[2]{
    {\section[#1]{\hyperref[sec:prob:#2]{#1}}\label{sec:sol:#2}}}

\usepackage[hyperindex]{hyperref}
\hypersetup{linktocpage=true,colorlinks,linkcolor=darkred,
            urlcolor=darkred,citecolor=darkred}

\begin{document}

\mytextstyle 
{\LARGE \textbf{Wrong Spacings}} 
%{\LARGE \textbf{Desired Spacings}} 
%\section{Section One}      <- activating this line 
%                           <- fixes spacing problem
%text
%\section{Section Two}
%text     


\myproblemstyle
\setcounter{section}{0}  
\bigskip\bigskip{\LARGE \textbf{Problems}}
\problemsection{Easy}{easy} 
text
\problemsection{Medium}{medium} 
text 
\problemsection{Difficult}{difficult}  
text 

\setcounter{section}{0}
\bigskip\bigskip{\LARGE \textbf{Solutions}}

\solutionsection{Easy}{easy}  
text
\solutionsection{Medium}{medium}  
text 
\solutionsection{Difficult}{difficult} 
text 

\end{document}  

MWE

I'm suspecting the problem lies in my definition of \problemsection and \solutionsection. Reason: if I avoid using hyperlinks, as in the code below (almost identical as that above, but using \section instead of \problemsection and \solutionsection), the spacing comes out correct regardless of whether the four TEXT lines are commented out or not (see right example). Any help in getting this straight would be much appreciated!!

\documentclass[openany]{book}
\usepackage{titlesec}
\usepackage{color}
\definecolor{darkred}{rgb}{0.7,0,0}
\setlength{\parindent}{0pt}
\pagestyle{empty}

\newcommand\mytextstyle{
\renewcommand{\thesection}{\Roman{section}} 
\titlespacing{\section}{0pt}{2.5\baselineskip}{\baselineskip}
}
\newcommand\myproblemstyle{
\renewcommand{\thesection}{\Alph{section}} 
\titlespacing{\section}{0pt}{1.4\baselineskip}{0.7\baselineskip}
}

% \newcommand{\problemsection}[2]{
%     {\section[#1]{\hyperref[sec:sol:#2]{#1}}\label{sec:prob:#2}}}
% \newcommand{\solutionsection}[2]{
%     {\section[#1]{\hyperref[sec:prob:#2]{#1}}\label{sec:sol:#2}}}

\usepackage[hyperindex]{hyperref}
\hypersetup{linktocpage=true,colorlinks,linkcolor=darkred,
            urlcolor=darkred,citecolor=darkred}

\begin{document}

\mytextstyle 
%{\LARGE \textbf{Wrong Spacings}} 
{\LARGE \textbf{Desired Spacings, but without hyperlinks}} 
% \section{Section One}  <- activating this line fixes spacing problem
% text
% \section{Section Two}
% text     


\myproblemstyle
\setcounter{section}{0}  
\bigskip\bigskip{\LARGE \textbf{Problems}}
\section{Easy}{easy} 
text
\section{Medium}{medium} 
text 
\section{Difficult}{difficult}  
text 

\setcounter{section}{0}
\bigskip\bigskip{\LARGE \textbf{Solutions}}

\section{Easy}{easy}  
text
\section{Medium}{medium}  
text 
\section{Difficult}{difficult} 
text 

\end{document}  
  • Why there are the additional braces (surrounding \section and \label) in the definition of \problemsection and \solutionsection? It seems to work if I remove them: \newcommand{\problemsection}[2]{\section[...]{...}\label{...}} – esdd Oct 20 '17 at 12:27
  • I've no idea why I had those braces in there - but you're right, removing them solved the problem. I'm impressed, thanks a lot! – jvd Oct 20 '17 at 12:50

Your Answer

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

Browse other questions tagged or ask your own question.