Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can I define a command like *, similar to

\newcommand{\astcommand}[1]{{#1}^*}

but which, when applied to a single symbol a, gives the output a*, and when applied to multiple symbols, like abc, automatically adds brackets to the output, like (abc)*?

I mean, without having to call it with brackets, as in

\astcommand{(abc)}

just

\astcommand{abc}
share|improve this question
1  
Welcome to TeX.SX! –  Heiko Oberdiek Aug 24 at 16:25
 
Thank you, @Heiko Oberdiek! –  Cristi Stoica Aug 24 at 17:41

2 Answers

up vote 4 down vote accepted

Here's a version with a *-variant that uses \mleft and \mright or an optional argument for manually sizing the parentheses:

\documentclass{article}
\usepackage{xparse,mleftright}

\ExplSyntaxOn

% pass control to an inner function
\NewDocumentCommand{\astcommand}{sO{}m}
 {
  \IfBooleanTF{#1}
   { \cstoica_astcommand_auto:n { #3 } }
   { \cstoica_astcommand_manual:nn { #2 } { #3 } }
 }

% *-version: we want to use \mleft(...\mright)
\cs_new_protected:Npn \cstoica_astcommand_auto:n #1
 {
  \_cstoica_astcommand:nnn { \mleft( } { #1 } { \mright) }
 }

% normal version; #1 is either empty or \big, \Big, ...
\cs_new_protected:Npn \cstoica_astcommand_manual:nn #1 #2
 {
  \_cstoica_astcommand:nnn { \mathopen{#1(} } { #2 } { \mathclose{#1)} }
 }

% #1 is the left delimiter, #2 is the string, #3 is the right delimiter
\cs_new_protected:Npn \_cstoica_astcommand:nnn #1 #2 #3
 {
  \int_case:nnF { \tl_count:n { #2 } }
   {% only one token, no fences
    {1}{ #2^{*} }
   }
   { #1 #2 #3^{*} } % more than one token
 }
\ExplSyntaxOff

\begin{document}

$\astcommand{a}\quad
 \astcommand{ab}\quad
 \astcommand{\sqrt{a}}\quad
 \astcommand{}$

$\astcommand[\big]{a}\quad\astcommand[\big]{ab}$

$\displaystyle\astcommand*{\frac{a}{b}}$

\end{document}

enter image description here

Caveat An input such as \astcommand{{abc}} will result in no parentheses. To force parentheses, add an empty group like in

\astcommand{{}a}
share|improve this answer
 
Thank you! I like how it detects \sqrt and \frac! –  Cristi Stoica Aug 24 at 17:50

Here's a solution using the xstring package

% arara: pdflatex
\documentclass{article}

\usepackage{xstring}

\newcommand{\astcommand}[1]{%
\StrLen{#1}[\mylength]%
\ifnum\mylength>1%
    (#1)^*%
\else%
    #1^*%
\fi}
\begin{document}

$\astcommand{a}$

$\astcommand{b}$

$\astcommand{abc}$

\end{document}

If you're likely to need the parenthesis to grow according to the argument, you might like to use \left and \right or look at the mleftright package.

share|improve this answer
 
Thanks, this is nice! Do you have any suggestion of how I could use it with for example $\astcommand{\tilde{a}}$, and $\astcommand{\sqrt{a}}$? –  Cristi Stoica Aug 24 at 17:43
 
@CristiStoica looks like egreg beat me to it :) glad you have a solution! –  cmhughes Aug 25 at 15:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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