调整章节标题并添加文字

调整章节标题并添加文字

我想写一个关于某些主题(在章节/小节中)当前状态的声明,与相应的章节/小节标题右对齐。例如,参见下图:

我这样做了: \section{Zeroth Topic} \vspace*{-2.25em} \hfill \textit{Not started} \\ Some text goes here.

但是有什么更简单的方法吗?我需要针对某些章节/小节标题的此类状态声明。这是一个最简单的工作示例:

\documentclass{article}

\begin{document}

    \section{Zeroth Topic} \vspace*{-2.25em} \hfill \textit{Not started} \\
    Some text goes here.

    \section{First topic}       \textit{Completed}
    Some text goes here.

    \section{Second topic}      \textit{Not started}
    Some text goes here.

    \section{Third topic}       % no comment
    Some text goes here.

    \section{Fourth topic}      % no comment
    Some text goes here.

        \subsection{First Subtopic}     \textit{Minor progress}
        Some text goes here.

        \subsection{Second Subtopic}            % no comment
        Some text goes here.

\end{document}

答案1

\section可以使用和命令的第二个可选参数轻松添加此功能\subsection,用于“状态”。

如果论点是空的,这并没有什么坏处。

\documentclass{article}

\usepackage{xparse}

\makeatletter
\let\latex@@section\section
\let\latex@@subsection\subsection




\RenewDocumentCommand{\section}{somO{}}{%
  \IfBooleanTF{#1}{% Is it the starred version (s)?
    \latex@@section*{#3}%
  }{% Nope
    \IfValueTF{#2}{% Is the 2nd optional argument in action?
      \latex@@section[#2]{#3\hfill\textit{#4}}
    }{% No 2nd optional argument, write `#3` to the ToC instead.
      \latex@@section[#3]{#3\hfill\textit{#4}}
    }%
  }%
}

\RenewDocumentCommand{\subsection}{somO{}}{%
  \IfBooleanTF{#1}{%
    \latex@@subsection*{#3}%
  }{%
    \IfValueTF{#2}{%
      \latex@@subsection[#2]{#3\hfill\textit{#4}}
    }{%
      \latex@@subsection[#3]{#3\hfill\textit{#4}}
    }%
  }%
}


\makeatother

\begin{document}
\tableofcontents

    \section{Zeroth Topic}[Not started]
    Some text goes here.

    \section{First topic}[Completed]
    Some text goes here.

    \section{Second topic}[Not started]
    Some text goes here.

    \section{Third topic}       % no comment
    Some text goes here.

    \section{Fourth topic}      % no comment
    Some text goes here.

    \subsection{First Subtopic}[Minor progress]

    Some text goes here.

    \subsection{Second Subtopic}
    Some text goes here.
\end{document}

在此处输入图片描述

更新

允许关闭内容的版本status

\documentclass{article}

\usepackage{xparse}

\makeatletter
\let\latex@@section\section
\let\latex@@subsection\subsection

\newif\ifusestatus

\usestatustrue




\RenewDocumentCommand{\section}{somO{}}{%
  \IfBooleanTF{#1}{%
    \latex@@section*{#3}%
  }{%
    \ifusestatus
    \edef\@@tempa@@{#4}
    \else
    \edef\@@tempa@@{}
    \fi
    \IfValueTF{#2}{%
      \latex@@section[#2]{#3\hfill\textit{\@@tempa@@}}
    }{%
      \latex@@section[#3]{#3\hfill\textit{\@@tempa@@}}
    }%
  }%
}

\RenewDocumentCommand{\subsection}{somO{}}{%
  \IfBooleanTF{#1}{%
    \latex@@subsection*{#3}%
  }{%
    \ifusestatus
    \edef\@@tempa@@{#4}
    \else
    \edef\@@tempa@@{}
    \fi
    \IfValueTF{#2}{%
      \latex@@subsection[#2]{#3\hfill\textit{\@@tempa@@}}
    }{%
      \latex@@subsection[#3]{#3\hfill\textit{\@@tempa@@}}
    }%
  }%
}


\makeatother

\begin{document}
\tableofcontents

    \section{Zeroth Topic}[Not started]
    Some text goes here.

    \section{First topic}[Completed]
    Some text goes here.

    \section{Second topic}[Not started]
    Some text goes here.

    \section{Third topic}       % no comment
    Some text goes here.

    \section{Fourth topic}      % no comment
    Some text goes here.

    \subsection{First Subtopic}[Minor progress]

    Some text goes here.

    \subsection{Second Subtopic}
    Some text goes here.
\end{document}

相关内容