TransWikia.com

Redefine choose as binom

TeX - LaTeX Asked by Another Grad student on August 18, 2021

I did not know that I should be using binom instead of choose (and I ignored all the warnings). Now, instead of manually changing it at all places where I used choose, I think there should be a way to redefine choose as binom right?

However, I don’t know how and cannot find how to do it.

One Answer

Assuming you always enclose the infix-style arguments of choose in curly braces, i.e., if you write ${n choose k}$ rather than just $n choose k$, the following LuaLaTeX-based approach may be of interest to you. It defines a Lua function and assigns it to LuaTeX's process_input_buffer callback to scan the input stream for instances of {...choose...} and to convert them to equivalent binom expressions before TeX starts its usual processing.

This approach does have a few limitations. In particular, it assumes that the two ... arguments of choose don't contain further curly braces. A less-binding constraint, hopefully, is that the instances of {...choose...} don't contain line breaks.

enter image description here

If you check the log file, you'll find that it doesn't contain any warnings of the type

Package amsmath Warning: Foreign command atopwithdelims;
(amsmath)                frac or genfrac should be used instead
(amsmath)                 on input line 13.

That's because by the time LaTeX processes the input, it never "sees" any of the choose instructions.

documentclass{article}
usepackage{amsmath} % for 'binom' macro
usepackage{luacode} % for 'luacode' environment
begin{luacode}
function choose2binom ( s )
   -- perform non-greedy pattern match for "1 or more instances of any character"
   return ( s:gsub ( "{(..-)choose(..-)}" , "binom{%1}{%2}" ) )
end
end{luacode}
AtBeginDocument{directlua{luatexbase.add_to_callback (
    "process_input_buffer", choose2binom , "choose2binom" ) }}

begin{document}
${5choose3}$ ${ 10 choose 4 }$ $ {nchoosek} $
end{document}

Answered by Mico on August 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP