행렬과 괄호 전치

행렬과 괄호 전치

transp나는 결국 하나의 인수, 즉 행렬의 이름과 마지막으로 두 개의 출력을 갖는 명령을 정의하고 싶습니다 .

  1. \transp{A}괄호 사이의 행렬 A^T입니다.
  2. \transp A는 단지 행렬 A^T입니다.

나는 다음 명령을 시도했습니다.

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

하지만 전치 기호를 인쇄하려면 를 써야 합니다 \transp{}. (2.에서 언급한 대로) 쓰기만 하기 위해 이전 명령을 수정할 수 있습니까 \transp?

답변1

표준 TeX 구문에 따르면 \transp{A}\transp A완전히 동일합니다.

~할 것 같다다음과 같은 방법으로 하세요:

\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}

하지만 혼란스럽기 때문에 피하겠습니다.

여기에 이미지 설명을 입력하세요

나는 다음이 훨씬 더 좋다고 생각합니다. 괄호를 추가하려는 위치를 명시적으로 표시합니다 *.

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

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

\begin{document}

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

\end{document}

답변2

다음은 작동하는 것 같지만 일반적으로 좋은 생각인지 의심됩니다. 일반적으로 \foo A인수 \foo {A}가 하나인 매크로에 대해 동일한 결과를 제공하며 인수가 둘 이상의 토큰으로 구성된 경우 중괄호가 필요합니다. 실제로 하나의 토큰만 포함하더라도 필수 인수에 중괄호를 사용하는 것이 좋은 습관이라고 말하고 싶습니다.

\transp중괄호가 없으면 하나의 토큰만 인수로 허용할 수 있으므로 \transp A+Bis \transp A및 도 마찬가지 입니다 +B. 특히 그러면 \transp \mathbf{A}끔찍하게 죽는다.

\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

별표가 표시된 변형이 더 일반적입니다(egreg의 답변도 참조).

\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}

p하지만 선택적 인수( 괄호, b대괄호) 를 사용할 수도 있습니다.

\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}

관련 정보