[Tex/LaTex] write $\log^2{x}$ in skmath

amsmathmath-modepackages

Standard LaTeX has the opportunity to write things like $\log^2{x}$. This is however no longer possible when loading the skmath package, because skmath expects the logarithm to be entered $\log[<base>]{<expression>}$.

Did I miss something or is it only possible to achieve this by re-redfining \log.

PS: I am aware that $\log{<expression>}^2 is mathematically the same and have set it thus for now, but the above method seems more reader-friendly to me.

Best Answer

One possible redefinition of the syntax, if you are willing to accept this syntax as a solution:

\log[<base>][<exponent>]{<expression>}

The required redefinition:

\ExplSyntaxOn
\RenewDocumentCommand\log{oom}{%
  \IfNoValueTF{#1}
    {\ensuremath{\__skmath_log:\IfNoValueTF{#2}{}{\c_math_superscript_token{#2}}\__skmath_parens:n{#3}}}
    {\ensuremath{\__skmath_log:\c_math_subscript_token{#1}\IfNoValueTF{#2}{}{\c_math_superscript_token{#2}}\__skmath_parens:n{#3}}}%
}
\ExplSyntaxOff

A complete document, with a multitude of test cases showing the behavior for omitted/empty arguments:

\documentclass{article}
\usepackage{skmath}

\ExplSyntaxOn
\RenewDocumentCommand\log{oom}{%
  \IfNoValueTF{#1}
    {\ensuremath{\__skmath_log:\IfNoValueTF{#2}{}{\c_math_superscript_token{#2}}\__skmath_parens:n{#3}}}
    {\ensuremath{\__skmath_log:\c_math_subscript_token{#1}\IfNoValueTF{#2}{}{\c_math_superscript_token{#2}}\__skmath_parens:n{#3}}}%
}
\ExplSyntaxOff

\begin{document}
\begin{gather}
  \log[10][2]{x} \\
  \log[10][]{x} \\
  \log[10]{x} \\
  \log[][2]{x} \\
  \log[][]{x} \\
  \log[]{x} \\
  \log{x} \\
  \log[10][2]{} \\
  \log[10][]{} \\
  \log[][2]{} \\
  \log[][]{}
\end{gather}
\end{document}

And its output:

enter image description here