带有可选参数的新命令定义中的条件结构存在错误

带有可选参数的新命令定义中的条件结构存在错误

我正在尝试创建一个新命令,用于向方程式添加标题和/或标签。非可选参数用于方程式的文本,第一个可选参数用于标签,第二个参数用于标题。我写了这段代码

    \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
        \begin{equation}
        \label{eq:#1}
        {(!!) #3}
        \end{equation}
        %\vspace{-1em}
    }{
        %IF CAPTION {#2} IS NOT EMPTY
        \begingroup
        \setlength{\abovecaptionskip}{0pt} 
        \begin{mycapequ}[H]
            \begin{equation}
            \label{eq:#1}
            {(!) #3}
            \end{equation}
            \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}

但是命令不能正常工作,事实上我期望在第二种情况下显示{(!!) #3}而不是{(!) #3}: 我认为可能的用法如下:

 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}}

答案1

\IfNoValueTF当您引用的参数被声明为 时,测试永远不会成立O,但只有当 时才成立o

我建议更改语法,因为连续两个可选参数很难管理。此外,#1根据代码,您的参数被假定为不为空。

\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
        \begin{equation}
        \IfValueT{#1}{\label{eq:#1}}
        #2
        \end{equation}
    }{%
        %IF CAPTION {#3} IS NOT EMPTY
        \begin{mycapequ}[H]
        \setlength{\abovecaptionskip}{0pt}%
        \begin{equation}
        \IfValueT{#1}{\label{eq:#1}}
        #2
        \end{equation}
        \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}

在此处输入图片描述

不过,我更喜欢

\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
        \begin{equation}
        \IfValueT{#1}{\label{eq:#1}}
        #2
        \end{equation}
    }{%
        %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}

因此您没有浮动问题。

相关内容