tcolorbox 内の \section{}

tcolorbox 内の \section{}

\section{}私は、チーム全体で使用される記事の付録用の LaTeX テンプレートを作成中です。現在の課題は、コマンドの品質を維持しながら、見出しの周りに色付きのボックスを作成することです\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}}

これにより、少なくとも\sectionboxToC にコンテンツが追加されますが、カウント数は追加されません。また、行数が多すぎて効率的ではありません。

ボックスを作成する他のさまざまな方法を含め、あらゆることを試したような気がしますが、うまくいきません。解決策を知っている人はいますか?

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

関連情報