Cor de fundo no modo quadro (titlesec)

Cor de fundo no modo quadro (titlesec)

É fácil obter a versão sem cor do meu cabeçalho \section usando o seguinte código:

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

Será parecido com: Cabeçalho \section sem cor

Quase consegui o que queria com este código

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

O que se parece com: Cabeçalho \section colorido

Embora próximos, existem dois problemas além do meu LaTeX-Fu atual; A falta de borda ao redor da parte principal e a natureza descentralizada do texto principal. Eu tentei uma variedade de \colorboxencantamentos \fcolorboxsem sorte, então me pergunto se alguém no tex.stack pode ajudar?

Juntos em um só lugar está meu 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}

Observo ao elaborar o MWE que minha versão não está devidamente alinhada como está a versão original. Adicione isso aos meus problemas…

Responder1

Aqui está um exemplo de como fazer isso. Use a explicitopção no titlesecpacote e defina o título em um arquivo parbox. Usando uma tikzimagem sobreposta para desenhar a caixa colorida do título. A altura do tikzdesenho foi calculada usando a caixa salva do título mais um pouco do comprimento preferido, que se sobreporá à caixa do rótulo da seção. Você pode ajustar alguns dos parâmetros de acordo com sua preferência. Desta forma, a versão em estrela do título da seção terá o mesmo formato da numerada.

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

insira a descrição da imagem aqui

informação relacionada