Text Formattingtitlesec | Colored Background extended to left Margin

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
voorneveld
Posts: 15
Joined: Mon Sep 07, 2009 11:18 am

titlesec | Colored Background extended to left Margin

Post by voorneveld »

In the MWE below, the header for the exercises of section 1 has a colored background and the text itself is centered.

Since exercise numbers are in the left margin, I would like to extend this colored background into the left margin only (not the right!) by a centimeter or two. And still keep the text "Exercises section~\thesection" centered.

Is there any way to do that? I tried adding negative space on the left with \titlespacing*, or extending \textwidth, but that doesn't do the trick.

Code: Select all

\documentclass{article}
\usepackage[explicit]{titlesec}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{ntheorem}

\newcommand\problems
{
\begingroup
\titleformat{\section}
    {\normalfont\sffamily\filcenter}
    {}
    {0pt}
    {\tikz[baseline]{\node[fill=blue!20,text=black,minimum width=\textwidth]{##1};}}
\section*{Exercises section~\thesection}
\endgroup
}

\theoremstyle{margin}
\theoremheaderfont{\small\bfseries}
\theorembodyfont{\small}
\theoremsymbol{}
\newtheorem{exercise}{}[section]

\begin{document}
\section{First section}
\lipsum[1]

\problems

\begin{exercise}
First question
\end{exercise}
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

titlesec | Colored Background extended to left Margin

Post by localghost »

Somehow I didn't manage to produce a heading that matches your wishes. But I had another idea which might attract your interest.

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}
\usepackage{ntheorem}
\usepackage{lipsum}

\newcounter{exsection}
\titleclass{\exsection}{straight}[\section]
\titleformat{\exsection}[leftmargin]
{\normalfont\color{blue!25}\titlerule[1ex]\vspace{2ex}\sffamily\bfseries\filleft}
{Exercise Section \theexsection}{3ex}{}[\vspace{3ex}]
\titlespacing{\exsection}{4pc}{3ex plus .1ex minus .2ex}{3pc}

\theoremstyle{margin}
\theoremheaderfont{\small\bfseries}
\theorembodyfont{\small}
\theoremsymbol{}
\newtheorem{exercise}{}[section]

\begin{document}
  \section{Foo}
    \lipsum[1]

  \exsection{Bar}

    \begin{exercise}
      \lipsum[2]
    \end{exercise}
\end{document}
I created a new "title class" \exsection as described in the titlesec manual with an own counter. You can use this heading style when ever you want to introduce a section with exercises. The heading is not centered but shifted into the margin. From my point of view this accords better with the style of your theorems. The new heading format might need some adjustment according to your page geometry


Thorsten
Attachments
The resulting output.
The resulting output.
titlesec-exercise-heading.png (120.82 KiB) Viewed 4321 times
voorneveld
Posts: 15
Joined: Mon Sep 07, 2009 11:18 am

titlesec | Colored Background extended to left Margin

Post by voorneveld »

Thanks, that looks really good! I tried a few other options and found a solution that is extremely ugly: I could extend stuff to the right, not to the left, so I shifted the text and background into the right margin and then used a negative shift in the \titlespacing...

It's not pretty, but it works!

Code: Select all

\documentclass{article}
\usepackage[explicit]{titlesec}
\usepackage{lipsum}
\usepackage{tikz}
\newcommand\ExcHeader
{
\begingroup
\titleformat{\section}%[display]
    {\normalfont\sffamily\filcenter}
    {test}
    {0pt}
    {\tikz[baseline]{\node[fill=blue!20,text=black,minimum width=25pt+\textwidth]{\hspace*{25pt}##1};}}
\titlespacing{\section}{-25pt}{5pt}{5pt}
\section*{Exercises section~\thesection}
\endgroup
}

% Note: I needed to shift by 25pt in three locations!

\usepackage{ntheorem}
\theoremstyle{margin}
\theoremheaderfont{\small\bfseries}
\theorembodyfont{\small}
\theoremsymbol{}
\newtheorem{exercise}{}[section]

\begin{document}
\section{A Test}
\lipsum[1]

\ExcHeader

\begin{exercise}[First exercise]
Question.
\end{exercise}

\begin{exercise}
Second question
\end{exercise}
\end{document}
Which looks like the attachment.
Attachments
titlesecexample.pdf
(56.25 KiB) Downloaded 516 times
Post Reply