3

I am trying to manually force big size for a \ket using the physics package, I am trying something along the lines:

\documentclass{article}
\usepackage{physics}
\begin{document}
$\ket\Big{a}$
\end{document}

I am trying this based on the physics package documentation where \qty\Big{} should results in big brackets/braces. However, my example above generates the following compilation error:

! Argument of \bBigg@ has an extra }.
<inserted text>
                \par
l.4 $\ket\Big
             {a}$

On the other hand, when I try $\abs\Big{a}$ it works just fine ... Not sure what I am missing here.

  • 1
    I don't think the physics package provides for that. On the other hand the definition of \ket is quite peculiar. – egreg Feb 27 '16 at 20:17
2

Ugly code for an ugly syntax. ;-)

Here are replacements for \bra and \ket that allow for specifying an explicit size for the delimiters.

I made no provision for things like

\BRA\big{x}\KET\Bigg{y}

and just the first size command is considered. If you like them, change

\NewDocumentCommand{\BRA}

into

\RenewDocumentCommand{\bra}

and similarly for \NewDocumentCommand{\KET}. Also change

  \peek_meaning_remove_ignore_spaces:NTF \KET

into

  \peek_meaning_remove_ignore_spaces:NTF \ket

and you'll have a full replacement.

Full code:

\documentclass{article}
\usepackage{physics}

\ExplSyntaxOn

\tl_new:N \l_braket_explicit_tl
\tl_new:N \l_braket_bra_tl
\bool_new:N \l_braket_resize_bool

\NewDocumentCommand{\BRA}{ l m }
 {
  \bool_set_true:N \l_braket_resize_bool
  \str_case:xnF { \tl_trim_spaces:n { #1 } }
   {
    { } { \tl_clear:N \l_braket_explicit_tl }
    { * } { \bool_set_false:N \l_braket_resize_bool }
   }
   {
    \bool_set_false:N \l_braket_resize_bool
    \tl_set:Nn \l_braket_explicit_tl { #1 }
   }
  \tl_set:Nn \l_braket_bra_tl { #2 }
  \peek_meaning_remove_ignore_spaces:NTF \KET
   {
    \BRAKETfinish
   }
   {
    \braket_bra:
   }
 }
\cs_generate_variant:Nn \str_case:nnF { x }

\cs_new_protected:Nn \braket_bra:
 {
  \bool_if:NTF \l_braket_resize_bool
   {
    \left\langle\l_braket_bra_tl\right\rvert
   }
   {
    \mathinner
     {
      \mathopen{\l_braket_explicit_tl\langle}
      \l_braket_bra_tl
      \mathclose{\l_braket_explicit_tl\rvert}
     }
   }
 }
\NewDocumentCommand{\BRAKETfinish}{ l m }
 {
  \bool_if:NTF \l_braket_resize_bool
   {
    \left\langle\l_braket_bra_tl\middle|#2\right\rangle
   }
   {
    \mathinner
     {
      \mathopen{\l_braket_explicit_tl\langle}
      \l_braket_bra_tl\l_braket_explicit_tl|#2
      \mathclose{\l_braket_explicit_tl\rangle}
     }
   }
 }

\NewDocumentCommand{\KET}{ l m }
 {
  \bool_set_true:N \l_braket_resize_bool
  \str_case:xnF { \tl_trim_spaces:n { #1 } }
   {
    { } { }
    { * } { \bool_set_false:N \l_braket_resize_bool }
   }
   {
    \bool_set_false:N \l_braket_resize_bool
    \tl_set:Nn \l_braket_explicit_tl { #1 }
   }
  \bool_if:NTF \l_braket_resize_bool
   {
    \left\lvert#2\right\rangle
   }
   {
    \mathinner
     {
      \mathopen{\l_braket_explicit_tl|}
      #2
      \mathclose{\l_braket_explicit_tl\rvert}
     }
   }
 }
\ExplSyntaxOff

\begin{document}

$a+\bra{x}+b$ $a+\bra*{x}+b$ $a+\bra{x}\ket{y}+b$ $a+\bra*{x}\ket*{y}+b$ $a+\ket{y}+b$

$a+\BRA{x}+b$ $a+\BRA*{x}+b$ $a+\BRA{x}\KET{y}+b$ $a+\BRA*{x}\KET*{y}+b$ $a+\KET{y}+b$

$\BRA\big{x}$ $\BRA\Big{x}\KET{y}$

\end{document}

enter image description here

You can surely notice that the original \bra*{x} command has wrong spacing, which is fixed in the new version.

1

You could enlist the help of the mathtools package and redefine \ket to make it recognize an optional explicit sizing directive.

enter image description here

\documentclass{article}

\usepackage{physics}
\let\ket\relax % undefine \ket

\usepackage{mathtools} % for "\DeclarePairedDelimiter" macro
\DeclarePairedDelimiter{\ket}{\lvert}{\rangle}

\begin{document}  % "\ket" autosizes the fences
$\ket{a} \quad \ket[\big]{a} \quad \ket[\Big]{a} \quad \ket*{\dfrac{a}{b}}$
\end{document}
  • Thanks for your answer, but this will break the purpose of the physics package. For example, with the physics package, I can say \bra{a}\ket{b} and it will automatically contract to <a|b>.... – Bichoy Feb 27 '16 at 20:23

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.