Monday, 27 May 2013

Provide boolean from two arguments case insensitives

Provide boolean from two arguments case insensitives

In most publications scientific names are abbreviated except the firs time. Moreover, the names must be in italics and only the first letter of the generic name should be upper-case.
The biocon package allow define a identifier for each specie that manage all these requirements, but I don't want depend of remember a identifier for each species. I want also include a well formatted nested index entry each time in the same step, that manually is a lot of code typing that obfuscated the body text.
So I decided to make my own macro for this in the form the command \species{Genus}{species}. The result is the MWE that worked reasonably, but I found some problems that seem related with etoolbox package:
1) \setbool inside tabulars do not work, failing to switch to the abbreviate name.
2) The arguments are case sensitive, but I cannot reduce all possible occurrences to obtain a unique boolean for each species. I tried \providebool{\lowercase{#1#2}} but do not work. This, I can correct case variations in the output but having a different boolean each time, so the macro cannot switch to the abbreviated form, and worse, produce a duplicated index entry.
\documentclass[11pt]{article}
\usepackage{etoolbox}
\usepackage{xstring}
\usepackage{mfirstuc}
\usepackage[colorlinks=true,linkcolor=blue]{hyperref}
\usepackage{makeidx}
\usepackage{biocon}
\makeindex

% Provide \genus{Generic name}
\newcommand\genus[1]}|textbf}}

% Provide \species{Generic name}{specific name}
\newcommand\species[2]{%
\providebool{#1#2}%
\ifbool{#1#2}%
% abbreviated name
{\emph{\uppercase{\StrLeft{#1}{1}}. \lowercase{#2}}}%
% full name
{\setbool{#1#2}{true}{\em\lowercase{\protect\makefirstuc{#1} #2}}}%
%index entry for both types
\index{#1@{\em\lowercase{\protect\makefirstuc{#1}}}!#2@{\em\uppercase{\protect\StrLeft{#1}{1}}. \lowercase{#2}}}}


\begin{document}

\noindent Desired ouput of some \genus{Homo} species:\\\\
\species{Homo}{erectus} \texttt{(full name 1st time, OK)}\\
\species{Homo}{erectus} \texttt{(abbreviation 2nd time, OK)}\\
\species{Homo}{sapiens} \texttt{(indexed as Homo!sapiens, OK)}\\
\species{Homo}{erectus} \\
\species{Homo}{sapiens} \texttt{(indexed as as Homo!sapiens too, OK)}\\

\noindent Testing in tabular environment:\\\\
\begin{tabular}{|l|l|}
\hline\species{Homo}{habilis} & \\
\hline\species{Homo}{habilis} & \texttt{(not working inside tabular)}\\\hline
\end{tabular}\\

\noindent\species{Homo}{habilis} method (try again):\\

\noindent\begin{tabular}{|l|l|}
\hline\species{Homo}{habilis} & \texttt{
(\textbackslash{}setbool worked only outside tabular)}\\\hline
\end{tabular}\bigskip

\noindent Testing case correction:\\
\verb|\species{Homo}{Antecessor}|: \species{Homo}{Antecessor}\\
\verb|\species{Homo}{antecessor}|: \species{Homo}{antecessor}\\
\verb|\species{HOMO}{ANTECEsSOR}|: \species{HOMO}{ANTECEsSOR}\\\\
Problems with these three equivalents commands:\\
\texttt{not abbreviations}\\
\texttt{duplicated "Homo" at index}\\
\texttt{duplicated "antecessor" at index}\\

\printindex
\end{document}

No comments:

Post a Comment