tcolorbox 中的 \section{}

tcolorbox 中的 \section{}

我正在為整個團隊使用的文章附錄製作 LaTeX 模板。我當前的挑戰是在標題周圍創建一個填充顏色的框\section{},同時保持命令的品質\section{}

我透過建立一個 來解決此任務tcolorbox,其外觀我很滿意,但該\section{}命令的功能已遺失。我需要一些幫助來解決這個問題。

我已將 tcolorboxes 命名為“ sectionboxes”,如下面的工作範例所示,並且希望能夠簡單地搜尋同事的文件並將全部替換\section{}\sectionbox{}應用此佈局。

我需要\sectionboxes 具有與\section命令相同的功能,即

  1. 在文字標題本身中顯示節數(例如“1.節標題”而不僅僅是“節標題”),以及

  2. 用於\sectionbox{}在目錄中顯示的內容及其對應的節數。

為了創建盒子,我使用了這個套件tcolorbox和下面的命令。

在文本中,我通常會寫\section,我寫:

\stepcounter{section}
\addcontentsline{toc}{section}{SECTION_NAME}
\sectionbox{\MakeUppercase{SECTION_NAME}}

這至少將\sectionbox內容添加到目錄中,但沒有計數。線路太多,效率低。

我覺得我已經嘗試了一切,包括許多其他創建盒子的方法,但我就是無法讓它發揮作用。有沒有人有辦法解決嗎?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tcolorbox}
\usepackage{lipsum}

\title{title}
\author{author}
\date{\today}

\newcommand{\sectionbox}[1] {
\begin{tcolorbox}
             [
              colback=purple!100,% background
              colframe=purple,% frame colour
              coltext=white, % text color
              width=\linewidth,%
              height=0.7cm, 
              halign=center,
              valign=center,
              fontupper=\large\bfseries,
              arc=0mm, auto outer arc,
             ]
    {#1}
\end{tcolorbox} 
} %

\begin{document}
\maketitle

\sectionbox{Contents}
\vspace{-1cm}
\renewcommand{\contentsname}{}

\tableofcontents
\newpage

\stepcounter{section}
\addcontentsline{toc}{section}{SECTION-NAME}
\sectionbox{\MakeUppercase{SECTION-NAME}}

\lipsum[1-2]

\end{document}

答案1

您所需要的只是\refstepcounter\numberline。提示,使用普通\section命令並比較 aux 檔案中的條目。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tcolorbox}
\usepackage{lipsum}

\title{title}
\author{author}
\date{\today}

\newcommand{\sectionbox}[1] {
\begin{tcolorbox}
             [
              colback=purple!100,% background
              colframe=purple,% frame colour
              coltext=white, % text color
              width=\linewidth,%
              height=0.7cm, 
              halign=center,
              valign=center,
              fontupper=\large\bfseries,
              arc=0mm, auto outer arc,
             ]
    {#1}
\end{tcolorbox} 
} %

\begin{document}
\maketitle

\sectionbox{Contents}
\vspace{-1cm}
\renewcommand{\contentsname}{}

\tableofcontents
\newpage

\refstepcounter{section}
\addcontentsline{toc}{section}{\string\numberline{\thesection}SECTION-NAME}
\sectionbox{\MakeUppercase{SECTION-NAME}}

\lipsum[1-2]

\end{document}

相關內容