TransWikia.com

Doubt about the xlop package and new commands

TeX - LaTeX Asked on July 14, 2021

I hope you are excellent. I am a basic education teacher and I am not very good at using LaTex, I would like to know how I could create a new command to compact the command that I use right now to do vertical basic addition and subtraction, I use the xlop package and the commands to add and subtract ( without the answer appearing) are the following:

$ opadd[resultstyle = gobble]{34}{58} $

$ opsub[resultstyle = gobble]{85}{35} $

Is it possible that I compact it as two commands of the type sum{}{} and sub{}{} where they appear to me in the same vertical way and that the answer is not written?
I appreciate so much your help and your time.

2 Answers

Of course, is possible, but without sum.

documentclass[twocolumn]{article}
usepackage{xlop,xcolor}
newcommandglobbe[1]{phantom{#1}}
newcommandxsum[2]{opadd[resultstyle=globbe]{#1}{#2}}
newcommandxsumr[2]{opadd[resultstyle=bfseriescolor{red!50!black}]{#1}{#2}}
begin{document}
    
opadd[resultstyle=globbe]{34}{58}

xsum{123}{321}

xsumr{123}{321}

paragraph{Note:} Change the macro name to what you  want,
                  except to those already in use, like  verb|sum| $=sum$ 

    
end{document}

mwe

Correct answer by Fran on July 14, 2021

What you're looking to do is create new commands. If you have an up-to-date LaTeX, you should be able to define your new commands with, e.g.,

NewDocumentCommand{Sum}{mm}{%
   ...
}

(we’ll get into what we put in the ... in a bit). If your LaTeX is older than last October, you would either have to use usepackage{xparse} to make available the current commands for defining new commands or you would use the old style for defining a two-argument command, which is:

newcommand{Sum}[2]{%
  ...
}

Note also, that I chose the name Sum for your command since sum is already in use to typeset the ∑ symbol.

I said two-argument command since you wanted to define a command with two arguments. With the newcommand style, you indicate you have two arguments by the optional argument which contains a number from 1–9 with the number of arguments your command has.¹

In the NewDocumentCommand style, we have a mandatory argument which gives a template for what arguments we will have. This enables us to do things like have optional arguments, or arguments with delimited text, or even verbatim arguments so you could make your own verb environment. There's a lot more, but I won't get into those now. Use texdoc xparse to get into the weeds if you find yourself called to do so.

Now for the inside part of our new command, what we put here will be the same for either style of definition. We put whatever we want the command to be using #1 to insert the contents of the first argument, #2 the second all the way up to #9 for the ninth argument. And now you know why we're limited to nine arguments. Otherwise, we would be stuck if we wanted to be able to have #10 mean the first argument followed by 0. So, for your example, you could replace the … with

$ opadd[resultstyle = gobble]{#1}{#2} $

although I would be inclined to instead write

opadd[resultstyle = gobble]{#1}{#2}

so I have the flexibility of putting my Sum in in-line math mode or display math mode.


  1. We'll see in a bit why we're limited to nine arguments. There are ways to work around this, but I won't get into that here.

Answered by Don Hosek on July 14, 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