Solución:
Puedes usar expl3
; los strcompare
macro es solo una versión de nivel de usuario de la ampliable función interna str_if_eq:nnTF
.
documentclass{article}
usepackage{expl3}
ExplSyntaxOn
cs_new_eq:NN strcompare str_if_eq:nnTF
ExplSyntaxOff
begin{document}
startpar
strcompare{abc}{def}{OOPS}{OK}par
strcompare{abc}{abcd}{OOPS}{OK}par
strcompare{abcd}{abc}{OOPS}{OK}par
strcompare{a}{}{OOPS}{OK}par
strcompare{}{a}{OOPS}{OK}par
end
medskip
startpar
strcompare{abc}{abc}{OK}{OOPS}par
strcompare{}{}{OK}{OOPS}par
end
end{document}
Lo he leído con usepackage{pdftexcmds}
, puedes usar [email protected]
pero no he comprobado si funciona en este caso.
Alternativamente, el código siguiente define [email protected]@equal{str1}{str2}{if-case}{else-case}
. Como @JavierBezos
anotado en los comentarios, los espacios no se tienen en cuenta (por lo que "a b"
= "ab"
), y una versión que los tenga en cuenta sería más difícil de lograr (¿si es posible?). Se puede utilizar de la siguiente manera:
makeatletter
start%
[email protected]@equal{abc}{def}{OOPS}{OK}%
[email protected]@equal{abc}{abcd}{OOPS}{OK}%
[email protected]@equal{abcd}{abc}{OOPS}{OK}%
[email protected]@equal{a}{}{OOPS}{OK}%
[email protected]@equal{}{a}{OOPS}{OK}%
end
start%
[email protected]@equal{abc}{abc}{OK}{OOPS}%
[email protected]@equal{}{}{OK}{OOPS}%
end
makeatother
Qué impresiones (observe que no se inserta ningún espacio extraño):
startOKOKOKOKOKend
startOKOKend
Aquí está la definición de [email protected]@equal
:
makeatletter
% #1 #2 #3 #4 sepend
% Params: {if-case}{else-case} sepr sepr sepend
% Or: {if-case}{else-case} B right-…(may be *nothing*)seprsepr sepend
def@[email protected]@[email protected]#1#2#3#4sepend{%
ifx#3sepr% #3right… = Empty string, a.k.a. "sepr"
% First case in the signature above (#3=sepr, #4=sepr)
#1% The strings are both empty, and therefore equal. Execute #1.
else%
% Second case in the signature above (#3=one char, #4=right…seprsepr)
#2% The right string is longer (there's at least one more character), so they are different. Execute #2.
fi%
}%
% #1 #2 #3 #4 sepl #5 #6 sepr #7 sepend
% Params: {if-case}{else-case}{left-first}{left-…} sepl sepr *nothing* sepr *nothing* sepend
% Or: {if-case}{else-case}{left-first}{left-…} sepl B right-…(may be *nothing*) sepr sepr sepend
def@[email protected]@[email protected]#1#2#3#4sepl#5#6sepr#7sepend{%
ifx#5sepr% #5#6 = Empty string, a.k.a. "sepr"
% First case in the signature above (#5=sepr, #6=*nothing*, #7=*nothing*)
#2% But the left string was not empty, so we run else-case
else%
% Second case in the signature above (#5=one char, #6=right…, #7=sepr)
ifx#3#5% First character of left == first character of right
@[email protected]@equal{#1}{#2}#4seplsepl#6seprseprsepend% Recursion on the rest
else%
#2% The strings are different at this character, execute #2.
fi%
fi%
}%
% Two possibilities:
% #1 #2 #3 #4 sepl #5 sepend
% @[email protected]@equal {if-case}{else-case} sepl *nothing* sepl <rest> sepend
% @[email protected]@equal {if-case}{else-case} A left-…(may be *nothing*) sepl sepl<rest> sepend
def@[email protected]@equal#1#2#3#4sepl#5sepend{%
ifx#3sepl% #3#4 = Empty string, a.k.a. "sepl"
% First case in the signature above (#3=sepl, #4=*nothing*, #5=<rest>)
@[email protected]@[email protected]{#1}{#2}#5sepend%
else%
% First case in the signature above (#3=one char, #4=left…, #5=sepl<rest>)
@[email protected]@[email protected]{#1}{#2}{#3}{#4}#5sepend%
fi%
}
def[email protected]@equal#1#2#3#4{%
% @[email protected]@equal#1sepl#4%#2sepl#4
%
% @[email protected]@equal{#4}seplsepl%
@[email protected]@equal{#3}{#4}#1seplsepl#2seprseprsepend%
}%
makeatother
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)