8

I would like to define a new operator for the range of a linear function. Since I am German I want to define it in a way that it changes depending on the used language.

\documentclass{scrartcl}
\usepackage[english]{babel}
\usepackage{amsmath}
\DeclareMathOperator{\range}{ran}
\DeclareMathOperator{\kernel}{ker}
% The following should be used if german or ngerman are defined for babel
% \DeclareMathOperator{\range}{Bild}
% \DeclareMathOperator{\kernel}{Kern}
\begin{document}
\begin{align}
  T &: V \rightarrow W \\
  \dim(V) &= \dim(\range(T)) + \dim(\kernel(T))
\end{align}
\end{document}

Is there an easy way to achieve this?

  • do you just want to make a choice at definition time or within a document always pick up the language current outside this math expression? – David Carlisle Jan 14 '17 at 12:30
  • Definition time is sufficient for this problem. – mcocdawc Jan 14 '17 at 12:31
  • It turned out to be easier to do the other way:-) – David Carlisle Jan 14 '17 at 13:02
9

In my opinion the definition should be so that the operator doesn't change across a document even through language changes.

So if the main language is German, \ker should resolve to “Kern” also in English. However, it's easy to provide also a “variable” kind.

\documentclass{scrartcl}
\usepackage[english,ngerman]{babel}
\usepackage{amsmath}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand\DeclareBabelMathOperator{mmO{???}}
 {
  \DeclareMathOperator{#1}
   {
    \str_case:xnF { \use:c { bbl@main@language } } { #2 } { #3 }
   }
 }
\NewDocumentCommand\DeclareVariableBabelMathOperator{mmO{???}}
 {
  \DeclareMathOperator{#1}
   {
    \str_case:xnF { \languagename }{ #2 } { #3 }
   }
 }
\cs_generate_variant:Nn \str_case:nnF { x }
\ExplSyntaxOff


\DeclareBabelMathOperator{\range}{
  {english}{ran}
  {ngerman}{Bild}
}
\DeclareBabelMathOperator{\kernel}{
  {english}{ker}
  {ngerman}{Kern}
}[ker]
\DeclareVariableBabelMathOperator{\vrange}{
  {english}{ran}
  {ngerman}{Bild}
}
\DeclareVariableBabelMathOperator{\vkernel}{
  {english}{ker}
  {ngerman}{Kern}
}[ker]

\begin{document}

\section{Fixed names}
\[
  \dim(V) = \dim(\range(T)) + \dim(\kernel(T))
\]
\begin{otherlanguage*}{english}
\[
  \dim(V) = \dim(\range(T)) + \dim(\kernel(T))
\]
\end{otherlanguage*}

\section{Variable names}
\[
  \dim(V) = \dim(\vrange(T)) + \dim(\vkernel(T))
\]
\begin{otherlanguage*}{english}
\[
  \dim(V) = \dim(\vrange(T)) + \dim(\vkernel(T))
\]
\end{otherlanguage*}

\end{document}

The optional argument is for the string to be used with a language not included in the previous choices.

enter image description here

Here is the output if english and ngerman are swapped throughout.

enter image description here

  • Thank you very much. That solves all possible cases, I could think of. – mcocdawc Jan 14 '17 at 17:09
7

enter image description here

\documentclass{scrartcl}
\usepackage[german,english]{babel}
\usepackage{amsmath}

\DeclareMathOperator{\range}{\rangename}
\DeclareMathOperator{\kernel}{\kernelname}

\addto\extrasenglish{%
\def\rangename{range}%
\def\kernelname{ker}%
}

\addto\extrasgerman{%
\def\rangename{Bild}%
\def\kernelname{Kern}%
}
\begin{document}

\selectlanguage{english}
english
\begin{align}
  T &: V \rightarrow W \\
  \dim(V) &= \dim(\range(T)) + \dim(\kernel(T))
\end{align}

\selectlanguage{german}
Und jetzt etwas auf Deutsch
\begin{align}
  T &: V \rightarrow W \\
  \dim(V) &= \dim(\range(T)) + \dim(\kernel(T))
\end{align}
\end{document}
  • Thank you very much for the great answer. I accepted egreg's answer because it solves both cases, but if possible I would accept all three – mcocdawc Jan 14 '17 at 17:08
  • @mcocdawc oh no! egreg will be unbearable in chat:-) – David Carlisle Jan 14 '17 at 17:11
2

I would stick to good old \operatorname and combine it with babel's \extrasLANGUAGE:

\documentclass{scrartcl}
\usepackage[german,english]{babel}
\usepackage{amsmath}

\addto\extrasenglish{%
    \def\range{\operatorname{ran}}%
    \def\kernel{\operatorname{ker}}%
}

\addto\extrasgerman{%
    \def\range{\operatorname{Bild}}%
    \def\kernel{\operatorname{Kern}}%
}
\begin{document}

English
\begin{align}
  T &: V \rightarrow W \\
  \dim(V) &= \dim(\range(T)) + \dim(\kernel(T))
\end{align}

\selectlanguage{german}
Und jetzt etwas auf Deutsch
\begin{align}
  T &: V \rightarrow W \\
  \dim(V) &= \dim(\range(T)) + \dim(\kernel(T))
\end{align}
\end{document}
  • Thank you very much for your help. I finally accepted egreg's answer because it solves both cases, but if possible I would accept all three – mcocdawc Jan 14 '17 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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