3

I would like to achieve the result of using the command \scell{111} and have the resultant output be $1\times 1\times 1$ so that it can look like 1x1x1 but with the x being a \times sign.

I wrote the basic command \newcommand{\scell}[3]{${#1}\times {#2}\times {#3}$} which obviously has to be input as \scell{1}{1}{1}. There are always 3 numerical single digit arguments, is there a way to make the command work by inputting simply \scell{111}?

3

Pass the argument to a three argument macro:

\documentclass{article}

\newcommand{\scell}[1]{\doscell#1}
\newcommand{\doscell}[3]{$#1\times#2\times#3$}

\begin{document}

\scell{111}

\scell{1{29}3}
\end{document}

enter image description here

This is a generalization:

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\scell}{m}
 {
  \seq_set_split:Nnn \l_tmpa_seq { } { #1 }
  $\seq_use:Nn \l_tmpa_seq { \times }$
 }
\ExplSyntaxOff

\begin{document}

\scell{111}

\scell{1{29}3}

\scell{3}

\scell{12}

\scell{12345}

\end{document}

enter image description here

5

With your existing definition you could use

\scell 1 1 1 

or even

\scell111 

In general {} may be omitted for single token arguments.

  • Oh I did not know this, thank you for sharing – Fiztban Aug 15 '15 at 16:26
  • I like to learn something new every day. Today, it was this. – Michael S Taylor Sep 25 '17 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.