Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In an answer to another question of mine, egreg posted code, which activates a character in math mode and gives it the function of \operatorname.

Now I want to put this into a package and set the character, that should be activated, in the package options. Just replacing the backtick with \shortoperator@char, it starts like this:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{shortoperator}[2014/02/09]

\RequirePackage{kvoptions}
\DeclareStringOption[`]{char}
\ProcessKeyvalOptions*

\RequirePackage{xparse,amsmath}

\ExplSyntaxOn
\tl_new:N \l__shortoperator_name_tl

\cs_new_protected:Npn \shortoperator_bq_mathop:
 {
  % clear the container
  \tl_clear:N \l__shortoperator_name_tl
  % start the recursion
  \shortoperator_absorb:
 }

\cs_new_protected:Npn \shortoperator_absorb:
 {
  \peek_catcode:NTF a
   {% if the next token is a letter absorb it
    \__shortoperator_absorb_next:n
   }
   {% otherwise produce the operator name
    \__shortoperator_deliver:
   }
 }

\cs_new_protected:Npn \__shortoperator_absorb_next:n #1
 {
  % add the next letter to the container
  \tl_put_right:Nn \l__shortoperator_name_tl { #1 }
  % restart the recursion
  \shortoperator_absorb:
 }

\cs_new_protected:Npn \__shortoperator_deliver:
 {
  % produce the operator name
  \operatorname{\l__shortoperator_name_tl}
 }

and continuous with:

% define the active back quote
\group_begin:
\char_set_catcode_active:N \shortoperator@char
\cs_gset_eq:NN \shortoperator@char \shortoperator_bq_mathop:
\group_end:

\ExplSyntaxOff

\AtBeginDocument{\mathcode`\shortoperator@char=\string"8000 }

where the option is used.

But of course that won’t work, because \shortoperator@char needs to be expanded. I tried to to use \expandafter to do that, but couldn’t get it to work. How can it be done?

An usage example (with XeLaTeX) would be:

\documentclass{article}
\usepackage[char=¡]{shortoperator}
\begin{document}
    \[e^{ix} = ¡cos x + i¡sin x\]
\end{document}
share|improve this question
    
egreg also explained that ¡ is two characters in UTF8 so the example you use cannot work, can it? XeLaTeX assumes UTF8 input as standard. –  cfr Feb 9 at 19:36
    
@cfr: When I put ¡ in place of \shortoperator@char it does work. I guess for XeLaTeX it’s just one character after all. –  canaaerus Feb 9 at 19:38

1 Answer 1

up vote 6 down vote accepted

You can't pass \shortoperator@char in the context of a number.

Here's a workaround. At the start of the package write

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{shortoperator}[2014/02/09]

\RequirePackage{kvoptions}
\DeclareStringOption[`]{char}
\ProcessKeyvalOptions*
\edef\shortoperator@char{`\shortoperator@char}

This makes \shortoperator@char into something usable after \mathcode; then, the final part should be

% define the active back quote
\group_begin:
\char_set_catcode_active:N \^^A
\char_set_lccode:nn { `\^^A } { \shortoperator@char }
\tl_to_lowercase:n
 {
  \cs_gset_eq:NN ^^A \shortoperator_bq_mathop:
 }
\group_end:

\ExplSyntaxOff

\AtBeginDocument{\mathcode\shortoperator@char=\string"8000 }

The \lowercase trick makes TeX “see” the (variable) character at the right spot.

You can make the whole package LaTeX3:

\NeedsTeXFormat{LaTeX2e}
\RequirePackage{expl3,l3keys2e,amsmath}
\ProvidesExplPackage{shortoperator}{2014/02/09}
  {v. 0.1}{Shorthand for operator names}

\keys_define:nn { shortoperator }
 {
  char .tl_gset:N = \g_shortoperator_char_tl,
  char .initial:n = { ` }, % default is the back quote
 }

\ProcessKeysPackageOptions{ shortoperator }

% normalize the token list
\tl_gput_left:Nn \g_shortoperator_char_tl { ` }

\tl_new:N \l__shortoperator_name_tl

\cs_new_protected:Npn \shortoperator_bq_mathop:
 {
  % clear the container
  \tl_clear:N \l__shortoperator_name_tl
  % start the recursion
  \shortoperator_absorb:
 }

\cs_new_protected:Npn \shortoperator_absorb:
 {
  \peek_catcode:NTF a
   {% if the next token is a letter absorb it
    \__shortoperator_absorb_next:n
   }
   {% otherwise produce the operator name
    \__shortoperator_deliver:
   }
 }

\cs_new_protected:Npn \__shortoperator_absorb_next:n #1
 {
  % add the next letter to the container
  \tl_put_right:Nn \l__shortoperator_name_tl { #1 }
  % restart the recursion
  \shortoperator_absorb:
 }

\cs_new_protected:Npn \__shortoperator_deliver:
 {
  % produce the operator name
  \operatorname{\l__shortoperator_name_tl}
 }

% define the active back quote
\group_begin:
\char_set_catcode_active:N \^^A
\char_set_lccode:nn { `\^^A } { \g_shortoperator_char_tl }
\tl_to_lowercase:n 
 {
  \cs_gset_eq:NN ^^A \shortoperator_bq_mathop:
 }
\group_end:

% at begin document, make the character math active
\AtBeginDocument
 {
  \char_set_mathcode:nn { \g_shortoperator_char_tl }{ "8000 }
 }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.