フレームモードの背景色 (titlesec)

フレームモードの背景色 (titlesec)

次のコードを使用すると、\section ヘッダーの色なしバージョンを簡単に取得できます。

{\normalfont}
{\filright
    \footnotesize
    \enspace SECTION \thesection\enspace
}
{8pt}
{\Large\bfseries\sectionfont\filcenter}

次のようになります: 色付けされていない \section ヘッダー

このコードでほぼ望み通りのものが手に入った

\titleformat{\section}
{\LARGE}
{\rlap
    {\color{Fraoch!30}
        \rule[-0.4cm]{\linewidth}{1.2cm}
    }
    {\normalfont}
    {\filright
        \raisebox{1em}{
            \fcolorbox{black}{white}{
                    \footnotesize
                    \enspace SECTION \thesection\enspace
                }
        }
    }
}
{1em}
{\Large\sectionfont\filcenter}

それは次のようになります: 色付きの\sectionヘッダー

近いですが、現在の LaTeX-Fu を超える 2 つの問題があります。メイン部分の周囲に境界線がないことと、メイン テキストが中央に配置されていないことです。さまざまな呪文を試しましたが\colorboxうまく\fcolorboxいきませんでした。tex.stack で誰か手助けできる人はいませんか?

私の MWE が 1 か所にまとめられています。

\documentclass{article}
\usepackage{fontspec}
\usepackage{lipsum}
\usepackage{enumitem}
\usepackage[table,x11names]{xcolor}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{xparse}
\usepackage{imakeidx}
\usepackage{bbding}
\usepackage{titlesec}
\usepackage[hyperindex]{hyperref}

\defaultfontfeatures{Ligatures=TeX}
%% \setmainfont{Hoefler Text}

\newcounter{cnt}
\setcounter{cnt}{0}

\NewDocumentCommand{\voc}{ m O{\O} O{#1}} {%
    \stepcounter{cnt}%
    \index[english]{#2|textit}
    \index{#3}%
    #1 & #2\\%
}
\newfontfamily\sectionfont{Goudy Bookletter 1911}
\definecolor{Fraoch}{named}{LightPink4}

\begin{document}
\titleformat{\section}[frame]
{\normalfont}
{\filright
    \footnotesize
    \enspace SECTION \thesection\enspace
}
{8pt}
{\Large\bfseries\sectionfont\filcenter}

{\setcounter{section}{9}
\section{UNIT 1 OF 9, English Translations}
\subsection{Intro}
\begin{tabular}{lr}
    \toprule
    \rowcolor{Fraoch!40}
    Gaelic & English\\
    \midrule
    \voc{Anna}[female name]
    \voc{Calum}[male name]
    \voc{Eilidh}[female name]
    \bottomrule
\end{tabular}
\titleformat{\section}
{\LARGE}
{\rlap
    {\color{Fraoch!30}
        \rule[-0.4cm]{\linewidth}{1.2cm}
    }
    {\normalfont}
    {\filright
        \raisebox{1em}{
            \fcolorbox{black}{white}{
                    \footnotesize
                    \enspace SECTION \thesection\enspace
                }
        }
    }
}
{1em}
{\Large\sectionfont\filcenter}
{\setcounter{section}{9}
\section{UNIT 1 OF 9, English Translations}
\subsection{Intro}
\begin{tabular}{lr}
    \toprule
    \rowcolor{Fraoch!40}
    Gaelic & English\\
    \midrule
    \voc{Anna}[female name]
    \voc{Calum}[male name]
    \voc{Eilidh}[female name]
    \bottomrule
\end{tabular}

\end {document}

MWE を作成する際に、私のバージョンが元のバージョンと適切に整合されていないことに気づきました。これが私の問題に加わります...

答え1

以下にその方法の例を示します。パッケージexplicitのオプションを使用してtitlesec、タイトルを で定義しますparbox。オーバーレイされたtikz画像を使用して、タイトルの色付きボックスを描画します。描画の高さは、tikzタイトルの保存されたボックスと、セクション ラベル ボックスと重なる希望の長さの一部を使用して計算されます。パラメータの一部を好みに合わせて調整できます。このようにして、セクション タイトルの星型バージョンは、番号付きバージョンと同じ形式になります。

\documentclass{article}
\usepackage{fontspec}
\usepackage{lipsum}
\usepackage{enumitem}
\usepackage[table,x11names]{xcolor}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{xparse}
\usepackage{imakeidx}
\usepackage{bbding}
\usepackage[explicit]{titlesec}
\usepackage[hyperindex]{hyperref}
\defaultfontfeatures{Ligatures=TeX}
%% \setmainfont{Hoefler Text}

\newcounter{cnt}
\setcounter{cnt}{0}

\NewDocumentCommand{\voc}{ m O{\O} O{#1}} {%
    \stepcounter{cnt}%
    \index[english]{#2|textit}
    \index{#3}%
    #1 & #2\\%
}
\newfontfamily\sectionfont{Goudy Bookletter 1911}
\definecolor{Fraoch}{named}{LightPink4}
\usepackage{tikz}
\usepackage{calc}
\titleformat{\section}[display]
{\footnotesize}
{%
\fcolorbox{black}{white}{%
\enspace SECTION \thesection\enspace}%
}
{2pt}
{%
\sbox0{\parbox{\textwidth}{\Large\sectionfont\filcenter#1}}%
\tikz[remember picture,overlay,blend mode=overlay]
{\draw[fill=Fraoch!40] (0.5\pgflinewidth,-\dp0-5pt) rectangle (\textwidth-0.5\pgflinewidth,\ht0+5pt);}%
\parbox{\textwidth}{\Large\sectionfont\filcenter#1}%
}
\begin{document}
\setcounter{section}{9}
\section{UNIT 1 OF 9, English Translations}
\subsection{Intro}
\begin{tabular}{lr}
    \toprule
    \rowcolor{Fraoch!40}
    Gaelic & English\\
    \midrule
    \voc{Anna}[female name]
    \voc{Calum}[male name]
    \voc{Eilidh}[female name]
    \bottomrule
\end{tabular}
\section{Example of a super super super super super super super super super long title}
\lipsum[1]
\section*{Example of a super super super super super super super super super long title without number}
\end {document}

ここに画像の説明を入力してください

関連情報