Skip to content

Parse variants of \cancel from the cancel package as non-standard symbolic units #817

@peaR-red

Description

@peaR-red

This TeX.SE post reveals that variants of \cancel from the cancel package, namely \bcancel and \xcancel, forces literal mode which leads to unwanted behavior. I've thus provided a workaround that parses the aforementioned variants in the same way as how the package parses \cancel, leveraging the internal control sequence \__siunitx_set_symbolic:Npnn found in siunitx-unit.dtx:

\documentclass{article}

\usepackage{siunitx,cancel}

\ExplSyntaxOn
\__siunitx_unit_set_symbolic:Npnn \bcancel
    { }
    { \__siunitx_unit_parse_special:n { \bcancel } }

\__siunitx_unit_set_symbolic:Npnn \xcancel
    { }
    { \__siunitx_unit_parse_special:n { \xcancel } }

\AtBeginDocument {
    \cs_if_exist:NT \bcancel
        {
            \cs_set_protected:Npn \__siunitx_vcancel:n #1
                { \__siunitx__real_bcancel:n { \__siunitx_print:nn { unit } {#1} } }
            \cs_set_eq:NN \__siunitx__real_bcancel:n \bcancel
            \seq_put_right:Nn \l_siunitx_unit_symbolic_seq { \bcancel }
            \cs_new_protected:cpn
                { __siunitx_ \token_to_str:N \bcancel _function:w }
                { \__siunitx_unit_parse_special:n { \__siunitx_bcancel:n } }
        }
    \cs_if_exist:NT \xcancel
        {
            \cs_set_protected:Npn \__siunitx_xcancel:n #1
                { \__siunitx__real_xcancel:n { \__siunitx_print:nn { unit } {#1} } }
            \cs_set_eq:NN \__siunitx__real_xcancel:n \xcancel
            \seq_put_right:Nn \l_siunitx_unit_symbolic_seq { \xcancel }
            \cs_new_protected:cpn
                { __siunitx_ \token_to_str:N \xcancel _function:w }
                { \__siunitx_unit_parse_special:n { \__siunitx_xcancel:n } }
        }
}
\ExplSyntaxOff

\begin{document}

\[
  \qty[per-mode=power]{2}{\cancel\metre\per\second}
\]

\[
  \qty[per-mode=power]{2}{\bcancel\metre\per\second}
\]

\[
  \qty[per-mode=power]{2}{\xcancel\metre\per\second}
\]

\end{document}

The resulting output is:

Image

Could this be added to siunitx?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions