我正在尝试定义一个新的子部分命令,其中带有标题的整行都是彩色的,并且我可以在其中提供额外的输入后小节标题,但与标题在同一行。我的问题是我没有正确处理副标题前的垂直间距。如果副标题跟在普通段落后面,我想添加空格,但如果副标题在分页符之后,则不应添加额外空格。
举例如下:
\documentclass{article}
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage{tabularx}
\usepackage{tikz}
\usepackage[table]{xcolor}
\renewcommand\tabularxcolumn[1]{m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash}p{#1}}
\definecolor{LightGrey}{gray}{.85}
\definecolor{Red}{rgb}{0.8,0.05,0.05}
\definecolor{Green}{rgb}{0.22,0.7,0.0}
\newcommand{\trafficlight}[2]{\textcolor{#1}{\begin{picture}(#2,#2)(0,0)\put(0,3.5){\circle*{#2}}\end{picture}}}
\lhead{}
\rhead{\begin{tabular}{r}\cr\nouppercase{\small\leftmark}\end{tabular}}
\lfoot{}
\cfoot{}
\rfoot{\small\thepage}
\pagestyle{fancy}
\newcommand{\mysubsection}[2]{
\ \\[1.5\baselineskip]
%\ \vspace{1.5\baselineskip}
\begin{tabularx}{\textwidth}{m{.97\textwidth}}
\cellcolor{LightGrey}
{
\begin{tabularx}{.95\textwidth}{m{.82\textwidth}R{.1\textwidth}}
{\hspace*{-3.5ex}
\refstepcounter{subsection}
\normalfont\large\bfseries\thesubsection{}\hspace*{2.5ex}{#1}}
&
{
\trafficlight{#2}{10}
}
\end{tabularx}
}
\end{tabularx}
% Manually add the section to toc-file
\addcontentsline{toc}{subsection}{\protect\numberline{\thesubsection}#1}
%
\\
}
\begin{document}
\tableofcontents
\section{A section}
Yada yada.
\subsection{Normal subsection}
Yada yada.
\mysubsection{My subsection}{Green}
Yada yada.
\newpage
\mysubsection{This is not at the top of the page}{Red}
Yada yada.
\end{document}
这是我的输出:
答案1
我想你想要类似的东西
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage{tabularx}
\usepackage{tikz}
\renewcommand\tabularxcolumn[1]{m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash}p{#1}}
\definecolor{LightGrey}{gray}{.85}
\definecolor{Red}{rgb}{0.8,0.05,0.05}
\definecolor{Green}{rgb}{0.22,0.7,0.0}
\newcommand{\trafficlight}[2]{\textcolor{#1}{\begin{picture}(#2,#2)(0,0)\put(0,3.5){\circle*{#2}}\end{picture}}}
\lhead{}
\rhead{\begin{tabular}{r}\nouppercase{\small\leftmark}\end{tabular}}
\lfoot{}
\cfoot{}
\rfoot{\small\thepage}
\addtolength\headheight{1pt}
\pagestyle{fancy}
\makeatletter
\let\normal@seccntformat\@seccntformat
\newcommand\boxed@seccntformat{%
\makebox[0pt][l]{\colorbox{LightGrey}{%
\hspace{-\fboxsep}\makebox[\linewidth][l]{%
\strut\hfill\trafficlight{\tlcolor}{10}}}}\normal@seccntformat}
\newcommand\mysubsection[2]{%
\let\@seccntformat\boxed@seccntformat
\def\tlcolor{#2}%
\subsection[#1]{#1}%
\let\@seccntformat\normal@seccntformat
}
\begin{document}
\tableofcontents
\section{A section}
Yada yada.
\subsection{Normal subsection}
Yada yada.
\mysubsection{My subsection}{Green}
Yada yada.
\newpage
\mysubsection{This is not at the top of the page}{Red}
Yada yada.
\end{document}