I currently have problems making the \rule operation be automated to be the same width as the title when the document is in \twocolumns. Is there a way for the grey box to follow the next line of the section title at all for multiline names? I would like to still use the package titlesec if possible. Thankyou!

This is the current code that I am using right now.The look of the subsection I currently have

\titleformat{\subsection}{\Large\sffamily\bfseries}{\rlap{\color{black!10}\rule[-0.255cm]{8.225cm}{0.9cm}} \thesubsection}{0.8em}{}

I have referred to this article which is the closest I can find to a solution but the title names do not align as I want it to.

Format section titles as white text on black background

Please help!

  • 1
    That's not what "rules" are for. It looks to me that you want to have background color for the section headings? Since you are running into issues with line breaking, you probably want to use tcolorbox to do the background color or use soul to do is as highlighting. See tex.stackexchange.com/questions/312574/… for some ideas. Commented Feb 26, 2021 at 20:15
  • Oh okay. Thank you so much for the suggestion!
    – Ms Thanh
    Commented Feb 27, 2021 at 22:31
  • @WillieWong I've been trying to incorporate the options into the section titles but it is not working. I have tried using \parbox but the parameter do not work within the \twocolumn layout. Can you help me? Thank you
    – Ms Thanh
    Commented Feb 28, 2021 at 0:45

1 Answer 1


EDIT: I have replaced the code with a slighly simpler version. The old version is in comments.

Here is something that works. It is a bit tricky to calculate the proper widths, but this does it. The trick is you have to pick up \linewidth (or \columnwidth) while in the twocolumn mode, to get the proper width.




% This is my original version
% \newcommand\mylabel{}
% \newcommand\myformat{\Large\sffamily\bfseries}

% \newcommand\formatmytitle[1]{%
%   \renewcommand\mylabel{\thesubsection\hspace{10pt}}
%   \setlength{\fboxsep}{5pt}%
%   \settowidth{\myboxwidth}{\myformat\mylabel}%
%   \setlength{\myboxwidth}{\dimexpr \linewidth -2\fboxsep-\myboxwidth}
%   \colorbox{black!10}{\mylabel{\parbox[t]{\myboxwidth}{\raggedright #1}}}}

% \titleformat{\subsection}[display]{\myformat}{}{0pt}{\formatmytitle}

% This is the new version, which is a bit simpler.

  \setlength{\myboxwidth}{\dimexpr \linewidth-2\fboxsep}
       \raggedright\leavevmode\box0 #1}}}}





\subsection{Subtracting Fractions}



enter image description here

  • Thank you so much! This is exactly what I needed! I tried using tikz as well but i think this is a simpler solution to my problem! @pietvo
    – Ms Thanh
    Commented Mar 1, 2021 at 16:29
  • but I realised it affected my Table of Contents when I switched over to your solution.
    – Ms Thanh
    Commented Mar 1, 2021 at 16:46
  • What is the problem with the table of contents? For me it looks normal. Commented Mar 1, 2021 at 20:09
  • The section number disappears for some reason but subsection and subsubsection is fine.
    – Ms Thanh
    Commented Mar 2, 2021 at 5:22
  • So I suppose you use the same solution for \section. When I do that, the section numbers are in the table of contents, so it must be some other code. I do have to add a \titleformat for unnumbered sections, however. If you still have problems with the section numbers in the table of contents, you may look into the titletoc package. Commented Mar 2, 2021 at 11:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .