制作一个简单的命令来生成命令的星号变体

制作一个简单的命令来生成命令的星号变体

根据指示的方法帖子中,我想创建一个\starredvariant命令,该命令可以接受一个命令并输出其带星号变体的定义。

重点是能够使用类似于的语法\newcommand,即使用括号作为参数分隔符和嵌入\WithSuffix

编辑。此类命令的目的不同于创建已加星标/未加星标从头开始配对(例如,使用类型为 的参数的布尔值s,如多个答案中所建议的那样)。整个要点是自定义一个不一定属于您的命令,或者您不想编辑其编码的命令。总之,这个想法是为了避免低级重新定义。

以下是我的想法:

\NewDocumentCommand{\starredvariant}{
m
O{}
O{}
m}
{\WithSuffix\newcommand#1*[#2][#3]{#4}}

微波能量吸收 1

\documentclass{article}
\usepackage{suffix}


\NewDocumentCommand{\starredvariant}{
    m
    O{}
    O{}
    m}
    {\WithSuffix\newcommand#1*[#2][#3]{#4}}

    
\newcommand{\inquotes}[2][]{#1 `#2'}

\starredvariant{\inquotes}[2][]{#1 «\,#2\,»}


\begin{document}
    
    \inquotes[fleur]{flower}
    
    \inquotes*[fleur]{flower}

    \inquotes*{flower}

\end{document}

在此处输入图片描述

但是,它在 MWE 2 中不能按预期工作。知道原因吗?

微波辐射计 2

\documentclass{article}
\usepackage{changepage}
\usepackage{suffix}
\NewDocumentCommand{\starredvariant}{
    m
    O{}
    O{}
    m}
{\WithSuffix\newcommand#1*[#2][#3]{#4}}



\newcommand{\tsltx}[1]{\normalfont\hspace{\parindent}`#1'}
\starredvariant{\tslt}[1]{\normalfont\hspace{\parindent}`#1'}

\begin{document}


\begin{adjustwidth}{7em}{}
    \tsltx{The Atlantic wears away our coasts. The pressure of the current from the Pole deforms our western cliffs. This wall that shields us from the sea is being undermined from Saint-Valery-sur-Somme to Ingouville; huge blocks of rock tumble down, the sea churns clouds of boulders, our harbors are silted up with sand and shingle, the mouths of our rivers are barred. Every day a stretch of Norman soil is torn away and disappears under the waves. This tremendous activity, which has now slowed down, has had terrible consequences. It has been contained only by that immense spur of land we know as Finistère. The power of the flow of water from the Pole and the violence of the erosion it causes can be judged from the hollow it has carved out between Cherbourg and Brest.
        
    The formation of this gulf in the Channel at the expense of French soil goes back before historical times; but the last decisive act of aggression by the ocean against our coasts can be exactly dated. In 709, sixty years before Charlemagne came to the throne, a storm detached Jersey from France.}
    
\end{adjustwidth}

\vspace{3ex} 

\begin{adjustwidth}{7em}{}
    \tslt*{The Atlantic wears away our coasts. The pressure of the current from the Pole deforms our western cliffs. This wall that shields us from the sea is being undermined from Saint-Valery-sur-Somme to Ingouville; huge blocks of rock tumble down, the sea churns clouds of boulders, our harbors are silted up with sand and shingle, the mouths of our rivers are barred. Every day a stretch of Norman soil is torn away and disappears under the waves. This tremendous activity, which has now slowed down, has had terrible consequences. It has been contained only by that immense spur of land we know as Finistère. The power of the flow of water from the Pole and the violence of the erosion it causes can be judged from the hollow it has carved out between Cherbourg and Brest.
        
    The formation of this gulf in the Channel at the expense of French soil goes back before historical times; but the last decisive act of aggression by the ocean against our coasts can be exactly dated. In 709, sixty years before Charlemagne came to the throne, a storm detached Jersey from France.}
    
\end{adjustwidth}
\end{document}

在此处输入图片描述

答案1

@Werner 的错误分析是完美的,问题在于你总是提供[]\newcommand而不是使用O{}下面的使用o和 检查参数是否存在。

请注意,我同意 Werner 和 egreg 的两个答案的观点,您应该更喜欢这个生成器宏s提供的 -argument ltcmd,以下内容只是为了向您展示您所犯的编码错误。

\documentclass{article}
\usepackage{changepage}
\usepackage{suffix}
\NewDocumentCommand{\starredvariant}{
    m
    o
    o
    m}
{%
  \IfNoValueTF{#3}%
    {%
      \IfNoValueTF{#2}%
        {\WithSuffix\newcommand#1*{#4}}%
        {\WithSuffix\newcommand#1*[#2]{#4}}%
    }%
    {\WithSuffix\newcommand#1*[#2][{#3}]{#4}}% braces around #3 so that it can contain `]`
}



\newcommand{\tsltx}[1]{\normalfont\hspace{\parindent}`#1'}
\starredvariant{\tslt}[1]{\normalfont\hspace{\parindent}`#1'}

\begin{document}


\begin{adjustwidth}{7em}{}
    \tsltx{The Atlantic wears away our coasts. The pressure of the current from the Pole deforms our western cliffs. This wall that shields us from the sea is being undermined from Saint-Valery-sur-Somme to Ingouville; huge blocks of rock tumble down, the sea churns clouds of boulders, our harbors are silted up with sand and shingle, the mouths of our rivers are barred. Every day a stretch of Norman soil is torn away and disappears under the waves. This tremendous activity, which has now slowed down, has had terrible consequences. It has been contained only by that immense spur of land we know as Finistère. The power of the flow of water from the Pole and the violence of the erosion it causes can be judged from the hollow it has carved out between Cherbourg and Brest.
        
    The formation of this gulf in the Channel at the expense of French soil goes back before historical times; but the last decisive act of aggression by the ocean against our coasts can be exactly dated. In 709, sixty years before Charlemagne came to the throne, a storm detached Jersey from France.}
    
\end{adjustwidth}

\vspace{3ex} 

\begin{adjustwidth}{7em}{}
    \tslt*{The Atlantic wears away our coasts. The pressure of the current from the Pole deforms our western cliffs. This wall that shields us from the sea is being undermined from Saint-Valery-sur-Somme to Ingouville; huge blocks of rock tumble down, the sea churns clouds of boulders, our harbors are silted up with sand and shingle, the mouths of our rivers are barred. Every day a stretch of Norman soil is torn away and disappears under the waves. This tremendous activity, which has now slowed down, has had terrible consequences. It has been contained only by that immense spur of land we know as Finistère. The power of the flow of water from the Pole and the violence of the erosion it causes can be judged from the hollow it has carved out between Cherbourg and Brest.
        
    The formation of this gulf in the Channel at the expense of French soil goes back before historical times; but the last decisive act of aggression by the ocean against our coasts can be exactly dated. In 709, sixty years before Charlemagne came to the throne, a storm detached Jersey from France.}
    
\end{adjustwidth}
\end{document}

答案2

不要将ltcmd(以前xparse)与混淆suffix

我不知道如何

\newcommand{\inquotes}[2][]{#1 `#2'}
\starredvariant{\inquotes}[2][]{#1 «\,#2\,»}

优于

\NewDocumentCommand{\inquotes}{sO{}m}{%
  #2 \IfBooleanTF{#1}{\frenchq}{\highq}{#3}%
}
\NewDocumentCommand{\frenchq}{m}{«\,#1\,»}
\NewDocumentCommand{\highq}{m}{`#1'}

或者,如果你想避免间接引用,

\NewDocumentCommand{\inquotes}{sO{}m}{%
  #2 \IfBooleanTF{#1}{«\,#3\,»}{`#3'}%
}

请注意,无论是否指定了可选参数,这都会在引用的内容前面添加一个空格。最后一个命令可能应该定义为

\NewDocumentCommand{\inquotes}{som}{%
  \IfValueT{#2}{#2 }%
  \IfBooleanTF{#1}{«\,#3\,»}{`#3'}%
}

答案3

\starredvariant在定义仅接受单个参数的命令时,您的命令会默默失败,并且实际上只适用于接受可选参数的命令。要了解原因,请考虑这个与您的第一个 MWE 类似的简单示例;人们会期望\abc{123}and\abc*{123}返回((123))and [[123]],但事实并非如此:

在此处输入图片描述

\documentclass{article}

\usepackage{suffix}

\NewDocumentCommand{\starredvariant}{ m O{} O{} m }{%
  \WithSuffix\newcommand#1*[#2][#3]{#4}}

\newcommand{\abc}[1]{((#1))}
\starredvariant{\abc}[1]{[[#1]]}

\begin{document}

\abc{123}

\abc*{123}

\end{document}

自从你一定O{}通过in将可选参数替换为(空白)默认值

\NewDocumentCommand{\starredvariant}{ m O{} O{} m }

你的\starredvariant\newcommand意志必然至少有两个参数规范。这反过来意味着你总是会有一个可选参数。为了使上述方法有效,你实际上必须使用

\abc{123}

\abc*[123]

由于您已经在使用 LaTeX3 定义,因此使用提供的布尔条件会更容易\IfBooleanTF{.}{<true>}{<false>}

\NewDocumentCommand{\abc}{ s m }{%
  \IfBooleanTF{#1}
    {((#2))}
    {[[#2]]}}

相关内容