如何使用条件(if 语句)和首字母缩略词中的数据?

如何使用条件(if 语句)和首字母缩略词中的数据?

我使用缩写词来自动完成我的文本,就像邮件功能一样。所以我一直想要使用缩写词的长文本,并且我使用它\acro{}{}来定义,所以\acro{Maria}{f}- 对我来说,显示叫玛丽亚的人是女性,我用它\ac{Maria}来自动完成我的文本。使用任何这些功能我都无法获得良好的结果。

我的代码:

\documentclass[a4paper,12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[portuguese]{babel}
\usepackage[T1]{fontenc}

\usepackage[dua,nolist]{acronym}
\usepackage{ifthen}
\usepackage{etoolbox}
\usepackage[T1]{fontenc}
\usepackage{hyperref}
\usepackage{ifthen}
\usepackage{xstring}

\begin{document}

\begin{acronym}
    \acro{a}{f} %f-female m-male
    \acro{b}{}  %none-male a-female
\end{acronym}

\IfStrEq{f}{\ac{a}}{female}{male}

\ifstrequal{\ac{a}}{f}{female}{male}

\ifblank{\ac{b}}{female}{male}

\ac{a}

\ac{b}

\end{document}

回答:

male
male
male
f

我猜想

female
female
female
f

答案1

您需要检索首字母缩略词的展开长格式并将其传递给条件。这里有一个想法(有几条注释试图解释代码):

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage[dua,nolist]{acronym}
\usepackage{etoolbox}

\makeatletter
% a macro that expands to nothing if the acronym isn't defined and to the
% long entry else -- it uses the fact that
%
%   \csname fn@<acro key>\endcsname
%
% expands (after two expansions) to two pairs of braces, the first containing
% the short entry and the second containing the long entry;
\newcommand*\aclongentry[1]{%
  % check if the acronym already exists (it does after the second compilation):
  \expandafter\ifx\csname fn@#1\endcsname\relax
    % and either gobble the group after the \fi ...
    \expandafter\@gobble
  \else
    % ... or just remove the braces
    \expandafter\@firstofone
  \fi
  {% two expansions need three (!) \expandafter
    \expandafter\expandafter\expandafter
    % use the second group containing the long form 
    \@secondoftwo\csname fn@#1\endcsname
  }%
}

% use etoolbox's tests on the long entry of an acronym -- usage:
%
%   \ifaclongtest{<conditional>}{<acro key>}<...>
%
% where <...> depends on the used conditional
\newcommand*\ifaclongtest[2]{%
  % we expand \romannumeral-`0\aclongentry{#2} as argument to
  % the conditional; \romannumeral-`0 triggers full expansion of the argument
  \expandafter#1\expandafter{\romannumeral-`0\aclongentry{#2}}%
}

\makeatother

\begin{document}

\begin{acronym}
    \acro{aaa}{f} %f-female m-male
    \acro{bbb}{}  %none-male a-female
\end{acronym}

aaa: \ifaclongtest{\ifstrequal}{aaa}{f}{female}{male}

bbb: \ifaclongtest{\ifstrequal}{bbb}{f}{female}{male}

aaa: \ifaclongtest{\ifblank}{aaa}{blank}{not blank}

bbb: \ifaclongtest{\ifblank}{bbb}{blank}{not blank}

\end{document}

经过两次编译后,结果如下

在此处输入图片描述

答案2

这次我刚开始发帖,但现在我正在使用

\newcommand \name \femalemale {f}

\IfStrEq {\femalemale}{f}{female}{male}

它解决了我的问题。

相关内容