TeX - LaTeX Asked on June 24, 2021
I’m trying to make a macro such that I give it arguments like this
sqrtnorm{x,y,z}
such that it expands to,
sqrt{x^2+y^2+z^2}
I’m trying (for no special reason) doing this with @for
command, this is what I’ve attempted
makeatletter
newcommandsqrtnorm[1]{
sqrt{
@forlevar:=#1do{
{levar}^2+}
}
}
makeatother
It’s perfect in every way, except that there is a little + hanging around at the end. Is there a way to solve this?
Instead of appending a +
I prepend a macro @tempa
, which defines itself as +
. In this way at the first term the definition is executed, and only from the second term the +
appears.
documentclass{article}
makeatletter
newcommand*{sqrtnorm}[1]{%
def@tempa{def@tempa{+}}%
sqrt{%
@forlevar:=#1do{@tempa{levar}^2}%
}%
}
makeatother
begin{document}
[
sqrtnorm{x,y,z}
]
end{document}
I've added a couple of %
to hide the end of line. That's not crucial here because the macro appears in math mode, but it's not a bad idea either (and saves token memory).
Answered by campa on June 24, 2021
With expl3
:
documentclass{article}
usepackage{amsmath}
ExplSyntaxOn
NewDocumentCommand{sqrtnorm}{m}
{
% make a sequence from the argument
seq_set_from_clist:Nn l_tmpa_seq { #1 }
% add ^2 to every item
seq_set_map:NNn l_tmpb_seq l_tmpa_seq { ##1^2 }
% deliver the sequence with + between items
sqrt{seq_use:Nn l_tmpb_seq { + }}
}
ExplSyntaxOff
begin{document}
$sqrtnorm{x,y,z}$
end{document}
Answered by egreg on June 24, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP