幀模式下的背景顏色(titlesec)

幀模式下的背景顏色(titlesec)

使用以下程式碼很容易獲得我的 \section 標題的未著色版本:

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

它看起來像: 未著色的\節標題

我幾乎已經用這段程式碼得到了我想要的東西

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

看起來像: 彩色\節標題

雖然很接近,但除了我目前的 LaTeX-Fu 之外還有兩個問題;主要部分周圍缺乏邊框,且正文不居中。我嘗試了各種\colorbox\fcolorbox咒語,但沒有成功,所以我想知道 tex.stack 上是否有人能夠伸出援手?

我的 MWE 集中在一個地方:

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

在此輸入影像描述

相關內容