# Avoid line break before acronym

I use the acronym package for acronyms. E.g., I would define a new acronym as \newacro{CDN}{content delivery network} and then use it by writing \ac{CDN}. The first occurrance of this prints: "content delivery network (CDN)"

Sometimes there is a line break before the abbreviation, e.g., after "network" and before "(CDN)". Is there any way to prevent line breaks here? I don't mind line breaks at other positions, e.g., between "content" and "delivery".

I know there are a lot of related questions, but none solves my problem.

• Please post a MWE. – TeXnician Feb 22 '17 at 15:26

The definition of \@acf which prints the acronym as Full (Short) is coded as

\newcommand*{\@acf}[1]{%
\ifAC@footnote
\acsfont{\AC@acs{#1}}%
\footnote{\AC@placelabel{#1}\AC@acl{#1}{}}%
\else
\acffont{%
\AC@placelabel{#1}\AC@acl{#1}%
\nolinebreak[3] %
\acfsfont{(\acsfont{\AC@acs{#1}})}%
}%
\fi
\ifAC@starred\else\AC@logged{#1}\fi}


The \nolinebreak[3] already discourages spacing but does not forbid it (see What is the difference between \nobreak and \nolinebreak?). To forbid breaking, \nolinebreak[4] or simply ~ can be used instead in the definition.

\documentclass{article}
\usepackage{acronym}

\makeatletter
\renewcommand*{\@acf}[1]{%
\ifAC@footnote
\acsfont{\AC@acs{#1}}%
\footnote{\AC@placelabel{#1}\AC@acl{#1}{}}%
\else
\acffont{%
\AC@placelabel{#1}\AC@acl{#1}%
\nolinebreak[3] %
\acfsfont{(\acsfont{\AC@acs{#1}})}%
}%
\fi
\ifAC@starred\else\AC@logged{#1}\fi}
\makeatother

\newacro{PDF}{Portable Document Format}

\begin{document}
Foooooooooooooooooooooooooooooooooooooo \ac{PDF}
\end{document}


Or to follow clemens' approach and do it a little less invasively

\usepackage{etoolbox}
\makeatletter
\patchcmd\@acf
{\AC@acl{#1}\nolinebreak[3] \acfsfont{(\acsfont{\AC@acs{#1}})}} % search
{\AC@acl{#1}\nolinebreak[4] \acfsfont{(\acsfont{\AC@acs{#1}})}} % replace
{} % success
{} % failure
\makeatother

• Unfortunately this gives me the error Command \@acf already defined. – CGFoX Feb 22 '17 at 16:34
• The first code snippet is just the definition of \@acf from acronym use either the \renewcommand code from the full document or the etoolbox code instead (I'm pretty sure neither of them should yield an already defined error). If you copy the full document I have in the answer does that compile for you? – Dai Bowen Feb 22 '17 at 16:43