\newcommand 中的条件格式

\newcommand 中的条件格式

我正在使用字典模板中的以下内容:

\newcommand{\entry}[4]{\framebox(7,7){}\hspace{.75em}\markboth{#1}{#1}\textbf{#1}\ {(#2)}\ \textit{#3}\ $\bullet$\ {#4}}

\entry{Purfling}{pur-fling}{Guitar Part}{Definition}

\entry{Neck}{}{}{Definition}

生产

Purfling (pur-fling) Guitar Part * Definition

Neck () * Definition

我希望第二个元素(括号)只有里面有文本时才出现:

Purfling (pur-fling) Guitar Part * Definition

Neck * Definition

答案1

如果参数 2 和/或 3 为空,则它们将被省略,连同后面的空格。

\documentclass{article}
\newcommand{\entry}[4]{\framebox(7,7){}\hspace{.75em}\markboth{#1}{#1}\textbf{#1}\ %
  \ifx\relax#2\relax\else{(#2)}\ \fi%
  \ifx\relax#3\relax\else\textit{#3}\ \fi%
  $\bullet$\ {#4}}
\begin{document}
\entry{Purfling}{pur-fling}{Guitar Part}{Definition}

\entry{Neck}{}{}{Definition}
\end{document}

在此处输入图片描述

答案2

当参数无效时,您还必须吞噬空格:

\documentclass{article}

\newcommand{\IfNotBlank}[2]{%
  \if\relax\detokenize{#1}\relax\else#2\fi
}

\newcommand{\entry}[4]{%
  \framebox(7,7){}%
  \hspace{.75em}%
  \markboth{#1}{#1}%
  \textbf{#1}\ %
  \IfNotBlank{#2}{(#2)\ }%
  \IfNotBlank{#3}{\textit{#3}\ }%
  $\bullet$\ {#4}%
}

\begin{document}

\entry{Purfling}{pur-fling}{Guitar Part}{Definition}

\entry{Neck}{}{}{Definition}

\end{document}

在此处输入图片描述

您可能需要查看一个键值接口:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\entry}{mm}
 {
  \group_begin:
  \keys_set:nn { kale/dict } { #1 }
  \framebox(7,7){} \hspace{.75em}
  \markboth{\l_kale_dict_main_tl}{\l_kale_dict_main_tl}
  \textbf{\l_kale_dict_main_tl}~
  \tl_if_blank:VF \l_kale_dict_hyphen_tl
   {
    (\l_kale_dict_hyphen_tl)~
   }
  \tl_if_blank:VF \l_kale_dict_note_tl
   {
    \textit{\l_kale_dict_note_tl}~
   }
  \textbullet{}~
  #2
  \group_end:
}

\keys_define:nn { kale/dict }
 {
  main   .tl_set:N = \l_kale_dict_main_tl,
  hyphen .tl_set:N = \l_kale_dict_hyphen_tl,
  note   .tl_set:N = \l_kale_dict_note_tl,
 }

\ExplSyntaxOff

\begin{document}

\entry{
  main=Purfling,
  hyphen=pur-fling,
  note=Guitar Part,
}{Definition}

\entry{
  main=Neck,
}{Definition}

\end{document}

答案3

这里有一些带有(没有 eTeX)过度软件的安全带:

\documentclass{article}
\makeatletter
%%----------------------------------------------------------------------
%% Check whether argument is empty:
%%......................................................................
%% \@CheckWhetherNull{<Argument which is to be checked>}%
%%                   {<Tokens to be delivered in case that argument
%%                     which is to be checked is empty>}%
%%                   {<Tokens to be delivered in case that argument
%%                     which is to be checked is not empty>}%
%%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
%%
%% A concern in his posting is that the argument is hit with \string
%% after some expansions which in edge cases might result in unbalancing
%% surrounding \if..\fi-constructs if the macro is used inside of such
%% \if..\fi-constructs.
%%
%% That challenging concern sickened me. ;-)
%%
%% Therefore I decided to implerment a variant where this cannot happen
%% as expansion is forced by \romannumeral:
%%
%% After the first expansion-step, \string is not applied yet.
%% After the second expansion-step, any possibly disturbing remainders
%% are already removed due to \romannumeral-expansion.
%%
%% No eTeX- or whatsoever extensions. No \if.. .Only \romannumeral,
%% digit 0, space token for terminating \romannumeral-expansion,
%% \string, \expandafter, \@firstoftwo, \@secondoftwo, {, }.
%%
%% May 20, 2016
%%
%% Ulrich Diez (e-mail: [email protected])
%%
\newcommand\@CheckWhetherNull[1]{%
  \romannumeral0\expandafter\@secondoftwo\string{\expandafter
  \@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
  \@secondoftwo\string}\expandafter\@firstoftwo\expandafter{\expandafter
  \@secondoftwo\string}\expandafter\expandafter\@firstoftwo{ }{}%
  \@secondoftwo}{\expandafter\expandafter\@firstoftwo{ }{}\@firstoftwo}%
}%
%%
%%----------------------------------------------------------------------
%% Check whether argument is blank (empty or only spaces):
%%......................................................................
%% -- Take advantage of the fact that TeX discards space tokens when
%%    "fetching" _un_delimited arguments: --
%% \@CheckWhetherBlank{<Argument which is to be checked>}%
%%                    {<Tokens to be delivered in case that
%%                      argument which is to be checked is blank>}%
%%                    {<Tokens to be delivered in case that argument
%%                      which is to be checked is not blank}%
\newcommand\@CheckWhetherBlank[1]{%
  \romannumeral\expandafter\expandafter\expandafter\@secondoftwo
  \expandafter\@CheckWhetherNull\expandafter{\@firstoftwo#1{}.}%
}%
%%----------------------------------------------------------------------
%%
\newcommand{\entry}[4]{%
  \framebox(7,7){}\hspace{.75em}\markboth{#1}{#1}\textbf{#1}\ %
  \@CheckWhetherBlank{#2}{}{(#2)\ }%
  \@CheckWhetherBlank{#3}{}{\textit{#3}\ }%
  $\bullet$\ {#4}%
}%
\makeatother
\begin{document}
\entry{Purfling}{pur-fling}{Guitar Part}{Definition}

\entry{Neck}{}{ }{Definition}
\end{document}

关键点是:

测试参数是否只包含空格标记或根本没有标记。不测试是否扩展论证产生了可见的材料,而不仅仅是胶水或“空虚”。

换句话说:像这样的事情\entry{Neck}{\empty}{\empty}{Definition} 可能需要另一条“安全带”。

相关内容