1

I would like to define a command transp having eventually one argument : the name of the matrix and finally two outputs.

  1. \transp{A} is the matrix A^T between parenthesis,
  2. \transp A is just the matrix A^T.

I tried this command :

\newcommand{\transp}[1]{
\ifstrempty{#1}{{}^{\text{\tbf{T}}} }{{}^{\text{\tbf{T}}} \left( #1 \right)}}

but to print the transpose symbol I have to write \transp{}. Can I modify the previous command in order to just write \transp (as mentioned in 2.) ?

  • 2
    Usually \foo A and \foo{A} are the same for a macro \foo taking an argument. So this is not easily done and would go against the normal behaviour (I don't want to say good practice, because I'm led to believe that it would be better practice to use braces even for one-token arguments). I think I saw a related question a while ago, but I can't find it now and it might have been about something else entirely. – moewe Feb 28 at 11:35
5

According to the standard TeX syntax, \transp{A} and \transp A are completely equivalent.

You might do in the following way:

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\DeclareRobustCommand{\transp}{%
  \@ifnextchar\bgroup\transp@paren\transp@simple
}
\newcommand{\transp@paren}[1]{(#1)^{T}}
\newcommand{\transp@simple}[1]{#1^{T}}
\makeatother

\begin{document}

$\transp A+\transp{B+C}$

\end{document}

but I would avoid it, because it's confusing.

enter image description here

I find the following much better. You explicitly mark where you want parentheses by adding *.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\NewDocumentCommand{\transp}{sm}{%
  \IfBooleanTF{#1}{(#2)^{T}}{#2^{T}}%
}

\begin{document}

$\transp{A}+\transp*{B+C}$

\end{document}
5

The following seems to work, but I doubt it is a good idea in general. Usually \foo A and \foo {A} give the same result for macros with one argument and the braces are needed in case the argument consists of more than one token. Indeed I would say that it is good practice to use braces for mandatory arguments even if they enclose only one token.

Note that \transp without braces can only accept one token as its argument, so \transp A+B is \transp A and +B. In particular then, \transp \mathbf{A} dies horribly.

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand*{\transp@nb}[1]{#1^{T}}
\newcommand*{\transp@br}[1]{(#1)^{T}}
\newcommand{\transp}{}
\protected\def\transp{%
  \@ifnextchar\bgroup
    {\transp@br}
    {\transp@nb}}
\makeatother

\begin{document}
  \begin{align*}
    \transp A \\
    \transp{A}
  \end{align*}
\end{document}

A^T//(A)^T

A starred variant would be more common (see also egreg's answer)

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand*{\transp@nb}[1]{#1^{T}}
\newcommand*{\transp@br}[1]{(#1)^{T}}
\newcommand{\transp}{}
\protected\def\transp{%
  \@ifstar
    {\transp@br}
    {\transp@nb}}
\makeatother

\begin{document}
  \begin{align*}
    \transp{A} \\
    \transp*{A}
  \end{align*}
\end{document}

but you could also use an optional argument (p for parentheses, b for brackets)

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\transp}[2][]{%
  \if#1p
    (#2)
  \else
    \if#1b
      [A]
    \else
     A
    \fi
  \fi^{T}
}
\makeatother

\begin{document}
  \begin{align*}
    \transp{A} \\
    \transp[b]{A}
  \end{align*}
\end{document}

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.