I have a document where paragraph-level headings are placed in the margins. I’ve implemented this using KOMAscript as in the following MWE:
\documentclass{scrartcl}
\RedeclareSectionCommand[afterskip=0pt]{paragraph}
\renewcommand{\sectioncatchphraseformat}[4]{%
\ifstr{#1}{paragraph}
{%
\makebox[0pt][r]{\raisebox{0pt}[\height][0pt]{\parbox[t]{2cm}{%
\raggedright\relax#4}%
}\hskip.25em\relax}%
}{%
\hskip #2#3#4%
}}
\usepackage{lipsum}
\begin{document}
\lipsum[2]
\paragraph{The name of this paragraph}
\lipsum[2]
\paragraph{Antidisestablishmentarianism}
\lipsum[2]
\end{document}
Unfortunately, this makes long words collide with the body text:
Since hyphenation looks ugly in this context, I’ve decided to solve this by pushing the paragraph down by a line when it would collide with the header. I’m currently using the following macro for this purpose:
\makeatletter
\newcommand{\overlongname}{%
\rule[-.5\baselineskip]{0pt}{0pt}%
\@afterindentfalse%
\@afterheading}
\makeatother
Issuing \overlongname after any problematic \paragraph{…} header is then sufficient to solve the problem.
However, I dislike the manual nature of this approach. Is there any way to automatically detect whether the section header horizontally overflows the parbox, and move the paragraph start when this occurs?

paragraphcommand with these approaches: https://tex.stackexchange.com/questions/18576/get-width-of-a-given-text-as-length then it's a matter of checking if it is larger than the margin width and if so, calling theoverlongnamecommand within theparagraphone. This is a suggestion, I am not sure if it will work. – Jes Sep 15 '23 at 14:46