使用 titlesec 调整超链接部分标题上方的间距

使用 titlesec 调整超链接部分标题上方的间距

我正在编写一个文本,后面跟着一个问题表,其中包含一组问题 A、B、C 和解决方案 A、B、C。每个问题和解决方案都在其自己的部分中。我使用 titlesec 包的 \titlespacing 命令格式化了部分标题上方和下方的间距,为文本和问题表创建了两种不同的样式,称为 \mytextstyle 和 \myproblemstyle。我还希望从问题 A 到解决方案 A 再返回等有超链接,因此定义了 \problemsection 和 \solutionsection 命令来处理超链接。但是,这有时会搞乱 \titlespacing 的功能。我在下面附上了三个示例输出:所需间距(左)、错误间距(中间)和所需间距但没有超链接(右)。中间的输出是通过注释掉在问题表开始之前创建一些文本的四行代码获得的——为什么这会改变后续间距对我来说是个谜。

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

平均能量损失

我怀疑问题出在我对 \problemsection 和 \solutionsection 的定义上。原因:如果我避免使用超链接,如下面的代码所示(与上面的代码几乎相同,但使用 \section 而不是 \problemsection 和 \solutionsection),无论四行文本是否被注释掉,间距都是正确的(参见正确的示例)。任何帮助解决这个问题的帮助都将不胜感激!!

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

相关内容