Motivation
I'd like a \pythagoras command that takes a list of expressions, with a second, optional list of bracket shapes, and returns a pythagorean expression.
Desired result
For instance,
\pythagoras{a,b,c}
should return something equivalent to
\sqrt{a^2+b^2+c^2}
while
\pythagoras{a,\frac{b}{2},c}[.,r,.]
should return something equivalent to
\sqrt{a^2+\left(\frac{b}{2}\right)^2+c^2}
Current code
\documentclass{article}
\usepackage{xparse,amsmath}
\ExplSyntaxOn
% count list
\NewDocumentCommand{\countlist}{m}{
\clist_count:n { #1 }
}
% repeat command
\cs_new_eq:NN \Repeat \prg_replicate:nn
% brackets; my notation of r for round, s for square, c for curly
\NewDocumentCommand{\br}{O{r} O{#1} m}{
\left
\str_case:nnF { #1 } { {r}{(} {s}{[} {c}{{} {v}{|} {V}{|} {.}{.} } {.}
#3
\right
\str_case:nnF { #2 } { {r}{)} {s}{]} {c}{}} {v}{|} {V}{|} {.}{.} } {.}
}
% pythagoras
\NewDocumentCommand{\pythagoras}{m O{.\Repeat{\countlist{#1}-1}{,.}}}{
\seq_set_from_clist:Nn \l_tmpa_seq {#1}
\seq_set_from_clist:Nn \l_tmpb_seq {#2}
\cs_set:Npn __mapper ##1##2 {__sep \tl_if_empty:nTF { ##2 }{##1}{\br[##2]{##1}}^2}
\cs_set:Npn __sep {\cs_set:Npn __sep {+}}
\seq_mapthread_function:NNN \l_tmpa_seq \l_tmpb_seq __mapper
}
\ExplSyntaxOff
Problems
My current attempt to create a default list of bracket shapes using \Repeat in the optional argument of \pythagoras doesn't seem to be working.
\begin{document}
\begin{align*}
& \pythagoras{x,y}[.,.] \\ % pass the bracket parameters explicitly; works as desired
& .\Repeat{\countlist{x,y}-1}{,.} \\ % the expression used in the definition; in the document, it works as desired
& \pythagoras{x,y} % rely on the \Repeat command in the optional argument in the definition;
% should return x^2+y^2;
% actually returns x^2
\end{align*}
\end{document}
I'm also not sure how to wrap the sum in a \sqrt. Wrapping the relevant line of the \pythagoras command returns an error.
Credits
