# \def, \edef math and commands

I have this command that's part of something bigger

\newcommand{\customParagraph}[2][1]{% Title, Name
\ifthenelse{\isempty{#1}}
{\def \customParTitle{}}
{\edef\customParTitle{ -- #1}}
\edef\customParTitle{#2\customParTitle} % This can be used by other commands to populate ToC
\subparagraph*{\customParTitle}
}


In my documents I call it like \customParagraph{Proof} or \customParagraph[Proof of something]{Proof} to get my subparagraphs. If I put some inline math in the first argument it's fine, but as soon as I put a command in there like \customParagraph[Proof of something in $\R$]{Proof}, where \R is defined as \newcommand*{\R}{\mathbb{R}}, I get an Undefined control sequence error at the corresponding line. Putting \mathbb{R} directly gives the same result.

How can I fix this command to get the expected result? Keep in mind that the definition of \customParTitle can be used as below

\addcontentsline{toc}{...}{\customParTitle}

• Can you please provide a complete minimal working example (MWE) that demonstrates the error? – Skillmon Sep 4 '18 at 11:06
• As always on this site, you are more likely to get help if you provide a full minimal example instead of just sniplets, then others can easily test your MWE – daleif Sep 4 '18 at 11:08
• You’re right but I was in a hurry. I’ll add it asap – Marco Boschi Sep 4 '18 at 11:09

Without a complete MWE I guess that it is caused by the multiple expansion of \customParTitle, so try changing the definition to the following (with this the arguments don't get expanded during the definition of \customParTitle):

\newcommand{\customParagraph}[2][1]{% Title, Name
\ifthenelse{\isempty{#1}}
{\def\customParTitle{}}
{\def\customParTitle{ -- #1}}%
\edef\customParTitle{\unexpanded{#2}\unexpanded\expandafter{\customParTitle}}% This can be used by other commands to populate ToC
\subparagraph*{\customParTitle}%
}


Embedded in a self-created MWE:

\documentclass[]{article}

\usepackage{amssymb}

\newcommand*\R{\mathbb{R}}

\newcommand\customParagraph[2][1]
{% Title, Name
\if\relax\detokenize{#1}\relax
\def\customParTitle{}%
\else
\def\customParTitle{ -- #1}%
\fi
\edef\customParTitle
{\unexpanded{#2}\unexpanded\expandafter{\customParTitle}}%
\subparagraph*{\customParTitle}%
}
\newcommand\YourCustomParagraph[2][1]
{% Title, Name
\if\relax\detokenize{#1}\relax
\def \customParTitle{}%
\else
\edef\customParTitle{ -- #1}%
\fi
\edef\customParTitle{#2\customParTitle}%
\subparagraph*{\customParTitle}%
}

\begin{document}
\customParagraph[Proof of something in $\R$]{Proof}%
%\YourCustomParagraph[Proof of something in $\R$]{Proof}% doesn't work
\end{document}