# Redefining macro clashes with titlesec package. How to avoid?

In my question here how to properly add to a macro without storing the original one in a tmp variable, I was told that my code is actually working. However, it somehow clashes with the titlesec package.

How can I change my code to properly work along the titlesec package while still not defining a tmp variable for the original macro?

\documentclass{article}

\usepackage{titlesec}

\def\abc{abc}
\begingroup\let\orgabc\abc\def\abc{\orgabc\endgroup def}

\begin{document}
\section{One}
\abc

\section{Two}
Two

\end{document}


Works fine with \usepackage{titlesec} commented out!

• Is that really the definition of \abc that you want, with or without the titlesec package it wil completely break latex as it puts \begin{document} inside a group, also if you use \abc more than once you will get an error over an unmatched \endgroup. Surely you do not want the \endgroup to be in the definition of \abc ? – David Carlisle Feb 28 '17 at 8:52
• @DavidCarlisle Is this a tex-core or a latex-base question? – errekak Feb 28 '17 at 22:05
• @erreka marginal either way but tex-core I think as mostly it's about \def and \endgroup – David Carlisle Feb 28 '17 at 22:09

\def\abc{abc}
\begingroup\let\orgabc\abc\def\abc{\orgabc\endgroup def}

\begin{document}
\section{One}
\abc


This would place \begin{document} inside a group started by \begingroup and ended by \abc.

Any environment other than document this would generate an error. It does not generate an error message here for technical reasons but it completely breaks all latex processing as all the setup that normally happens at \begin{document} will be discarded at \abc

Depending on the intention you could do

\def\abc{abc}
\let\orgabc\abc
\def\abc{\orgabc def}

\begin{document}
\section{One}
\abc


or

\def\abc{abc}
\expandafter\def\expandafter\abc\expandafter{\abc def}

\begin{document}
\section{One}
\abc


or

\makeatletter
\def\abc{abc}
\makeatother

\begin{document}
\section{One}
\abc


Actually your code is equivalent to

\documentclass{article}
\usepackage{titlesec}

\begingroup
\begin{document}
\section{One}
\endgroup

\section{Two}
Two

\end{document}


but titlesec use \AtBeginDocument{...\let\ttl@Hy@refstepcounter\refstepcounter...} so after the \endgroup i.e after \abc in your code \ttl@Hy@refstepcounter is no more defined.

• breaking titlesec is the least of the problems, it means any definitions that happen at begin document are discarded at the group end, so default document fonts, math setup, any \label information from the aux file.... – David Carlisle Feb 28 '17 at 8:55