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.
\documentclass{article}
\usepackage{titlesec}
\usepackage{lipsum}
\usepackage{xcolor}
\newlength\myboxwidth
% 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.
\newcommand\formatmytitle[1]{%
\setlength{\fboxsep}{5pt}%
\setlength{\myboxwidth}{\dimexpr \linewidth-2\fboxsep}
\colorbox{black!10}{{\parbox[t]{\myboxwidth}{%
\setbox0\hbox{\thesubsection\hspace{0.5em}}\hangindent\wd0\hangafter1
\raggedright\leavevmode\box0 #1}}}}
\titleformat{\subsection}[display]{\Large\sffamily\bfseries}{}{0pt}{\formatmytitle}
\begin{document}
\twocolumn
\sloppy
\section{Whatever}
\subsection{Subtracting Fractions}
\lipsum[1-4]
\end{document}

tcolorbox
to do the background color or usesoul
to do is as highlighting. See tex.stackexchange.com/questions/312574/… for some ideas.