TeX - LaTeX Asked by saarl on May 26, 2021
I’m using the package cleveref
to automatically print the name of the kind of "theorem" of a reference via the Cref
macro. Since I’m writing in Spanish, the most natural way to refer to a theorem is by adding a definite article before the reference (that is, I want to refer to "the Theorem 1.1" instead of just "Theorem 1.1"). The problem is that in Spanish, the form of the definite article depends on the grammatical gender of the word it modifies.
So let’s say I have something like this:
documentclass{article}
usepackage[spanish]{babel}
usepackage{ifthen}
usepackage{amsthm}
usepackage{cleveref}
theoremstyle{definition}
newtheorem{teo}{Teorema}
newtheorem{prop}{Proposición}
begin{document}
begin{teo} label{first}
A theorem
end{teo}
begin{prop} label{second}
A proposition
end{prop}
El Cref{first}, el Cref{second}.
end{document}
The output looks like this:
Teorema 1. A theorem
Proposición 1. A proposition
El Teorema 1, el Proposición 1.
"teorema" means ‘theorem’, "proposición" means ‘proposition’, and "el" means ‘the’. However, since "proposición" is a feminine noun, it takes the feminine article "la" instead of "el". So the last line should be El Teorema 1, la Proposición 1
. I want a way to do this automatically, so that somemacro{ref}
prints el Teorema n
if the ref
points to a theorem and la Proposición n
if ref
points to a proposition. I tried adding the following to the preamble:
newcommand{ElCref}[1]{% capitalized
ifthenelse{%
equal{nameCref{#1}}{Proposición}%
}{%
La Cref{#1}%
}{%
El Cref{#1}%
}%
}
newcommand{elCref}[1]{% non-capitalized
ifthenelse{%
equal{nameCref{#1}}{Proposición}%
}{%
la Cref{#1}%
}{%
el Cref{#1}%
}%
}
and then ElCref{first}, elCref{second}.
inside the document. However, this produces the same output as before. More specifically, it looks like ifthenelse{equal{nameCref{ref}}{Name}}{1}{0}
always evaluates to 0
, even though nameCref
seems to be working fine. I gather this probably has to do with how TeX does string comparisons, but I don’t know enough to figure out how to fix it.
Use crefformat
(for the lower case version) and Crefformat
to add the correct article.
documentclass{article}
usepackage[spanish]{babel}
usepackage{amsthm}
usepackage{cleveref}
newtheorem{teo}{Teorema}
newtheorem{prop}{Proposición}
Crefformat{teo}{El #2teorema~#1#3}
crefformat{teo}{el #2teorema~#1#3}
Crefformat{prop}{La #2proposición~#1#3}
Crefformat{prop}{la #2proposición~#1#3}
begin{document}
begin{teo} label{first}
A theorem
end{teo}
begin{prop} label{second}
A proposition
end{prop}
Cref{first}, cref{second}.
end{document}
Correct answer by egreg on May 26, 2021
Your best bet is to use the crefformat
commands. e.g.,
crefformat{teo}{el teorema}
Crefformat{teo}{El teorema}
and the base cref
/Cref
commands for the references.
As for why your attempt doesn't work, it's because the nameCref
command does not expand at the top level to the expected string, but rather to a series of commands that ultimately gives the string. This is a common issue for people trying to do string comparisons on the results of LaTeX macros.
Answered by Don Hosek on May 26, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP