# Bugs in conditional structure in a newcommand definition with optional arguments

I am trying to create a new command for add a caption and/or the label to an equation. The not optional parameter is for the text of the equation and first optional parameter is for the label, the second one for the caption. I wrote this code

    \documentclass[11pt,a4paper]{article}
\usepackage[italian]{babel}
\usepackage[left=2.50cm, right=2.50cm, top=2.50cm, bottom=2.50cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{float}
\usepackage{caption}
\usepackage{xparse}

\DeclareCaptionType{mycapequ}[][List of equations]
\captionsetup[mycapequ]{labelformat=empty,textfont={bf,it}}

\renewcommand{\theequation}{\thesection.\arabic{equation}}%

\DeclareDocumentCommand{\myeq}{ O{} O{} m }{
%\newcommand{\myeq}[3][]{

\IfNoValueTF{#2}{
%IF CAPTION {#2} IS EMPTY
$$\label{eq:#1} {(!!) #3}$$
%\vspace{-1em}
}{
%IF CAPTION {#2} IS NOT EMPTY
\begingroup
\setlength{\abovecaptionskip}{0pt}
\begin{mycapequ}[H]
$$\label{eq:#1} {(!) #3}$$
\caption{{#2}}
\end{mycapequ}
\endgroup

}
}

\begin{document}

\myeq[][Forma finita di Eulero]{U\left(S,V,N\right)=TS-pV+\mu N}
text
\myeq[][]{\sideset{_{(ir)rev}}{}\oint \frac{\delta Q}{T}\le 0}
text
\end{document}


but the command does not work properly, infact I expect that was shown {(!!) #3} and not {(!) #3} in the second case: The possible usages I have thought are these:

 1. \myeq{\vec{F}=m*\vec{a}}
2. \myeq[labe1][]{\vec{F}=m*\vec{a}}
2. \myeq[][2nd Newton's Law]{\vec{F}=m*\vec{a}}
3. \myeq[labe1][2nd Newton's Law]{\vec{F}=m*\vec{a}}

• You don't check whether the label is empty. Why that? – Skillmon Nov 12 '17 at 19:00
• you have not shown the use or the error you got, but \newcommand can only define commands with one optional argument so the usage is \myeq{caption}{equation} or \myeq[label]{caption}{equation} your description appeared to imply you are using it as if it had two optional arguments – David Carlisle Nov 12 '17 at 19:00
• Also, what is the definition of \ifemptyarg? Please provide a complete minimal working example (MWE) that starts at \documentclass and ends at \end{document} being as minimal as possible to recreate your issue. – Skillmon Nov 12 '17 at 19:07
• In my experience, there is a difference between \empty and \@empty, and []  is the same as \@empty. I usually set \empty as the default value so that \ifx\empty#1\relax will work. – John Kormylo Nov 12 '17 at 19:15
• note also that the blank line at the start of your definition is forcing a paragraph break before the equation, but you should never have a paragraph break before a display (TeX never makes reasonable spacing in that case) – David Carlisle Nov 12 '17 at 20:00

The test \IfNoValueTF is never true when the argument you refer to is declared as O, but only with o.

I suggest a change in syntax, because two optional arguments in a row are difficult to manage. Moreover, your argument #1 is assumed to be not empty, given the code.

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{amsmath}
\usepackage{caption,float}
\usepackage{xparse}

\DeclareCaptionType{mycapequ}[][List of equations]
\captionsetup[mycapequ]{labelformat=empty,textfont={bf,it}}

\numberwithin{equation}{section}

\NewDocumentCommand{\myeq}{ o m o }{%
\IfNoValueTF{#3}{%
%IF CAPTION {#3} IS EMPTY
$$\IfValueT{#1}{\label{eq:#1}} #2$$
}{%
%IF CAPTION {#3} IS NOT EMPTY
\begin{mycapequ}[H]
\setlength{\abovecaptionskip}{0pt}%
$$\IfValueT{#1}{\label{eq:#1}} #2$$
\caption{{#3}}
\end{mycapequ}
}
}

\begin{document}

\myeq{U\left(S,V,N\right)=TS-pV+\mu N}[Forma finita di Eulero]
text
\myeq[y]{\sideset{_{(ir)rev}}{}\oint \frac{\delta Q}{T}\le 0}
text and reference~\ref{eq:y}

\end{document}


However, I'd prefer something like

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{amsmath}
\usepackage{xparse}

\numberwithin{equation}{section}

\NewDocumentCommand{\myeq}{ o m o }{%
\IfNoValueTF{#3}{%
%IF CAPTION {#3} IS EMPTY
$$\IfValueT{#1}{\label{eq:#1}} #2$$
}{%
%IF CAPTION {#3} IS NOT EMPTY
\begin{gather}
\IfValueT{#1}{\label{eq:#1}}
#2\\
\textbf{\itshape #3}\notag
\end{gather}
}
}

\begin{document}

text
\myeq{U\left(S,V,N\right)=TS-pV+\mu N}[Forma finita di Eulero]
text
\myeq[y]{\sideset{_{(ir)rev}}{}\oint \frac{\delta Q}{T}\le 0}
text and reference~\ref{eq:y}

\end{document}


so you have no float problem.