3

Following this answer, I wrote a command that allows me to write \cont*{(D_\mu\phi)} to get the effect of (D_\mu\phi)^\dagger(D^\mu\phi).

\def\replace#1#2#3{%
  \def\tmp##1#2{##1#3\tmp}%
    \tmp#1\stopreplace#2\stopreplace}
\def\stopreplace#1\stopreplace{}

\makeatletter
\newcommand{\cont}{\@ifstar\@@cont\@cont}
\newcommand{\contsubst}[1]{\replace{#1}{_\mu}{^\mu}}
\def\@cont#1{{#1}\contsubst{#1}}
\def\@@cont#1{{#1}^\dagger\contsubst{#1}}
\makeatother

The problem is that this obviously doesn't work if \mu is inside curly braces, for things like F_{\mu\nu}.

I tried defining \contsubst like this

\edef\LB{\string{}
\newcommand{\contsubst}[1]{\replace{\replace{#1}{_\mu}{^\mu}}{_\LB\mu}{^\LB\mu}}

but that doesn't work.

Edit:

Here are some more examples of what the command should do:

\cont{A_\mu}  ->  A_\mu A^\mu
\cont{F_{\mu\nu}}  ->  F_{\mu\nu} F^{\mu\nu}
\cont*{\phi}  ->  \phi^\dagger \phi
\cont*{(D_\mu\phi_1)}  ->  (D_\mu\phi_1)^\dagger (D^\mu\phi_1)  % subscript 1 stays a subscript
\cont*{\Phi_{\mu\nu}}  ->  \Phi_{\mu\nu}^\dagger \Phi^{\mu\nu}
SU3
  • 519

3 Answers3

3

One option would be to use etoolbox and patch the mandatory argument, replacing _ with ^:

enter image description here

\documentclass{article}

\usepackage{xparse,etoolbox}

\NewDocumentCommand{\cont}{s m}{%
  \def\mArg{#2}
  \patchcmd{\mArg}{_}{^}{}{}% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
  #2
  \IfBooleanT{#1}{^\dagger}
  \mArg
}

\begin{document}

$\cont{A_\mu}$ % A_\mu A^\mu

$\cont{F_{\mu\nu}}$ % F_{\mu\nu} F^{\mu\nu}

$\cont*{\phi}$ % \phi^\dagger \phi

$\cont*{(D_\mu\phi_1)}$ % (D_\mu\phi_1)^\dagger (D^\mu\phi_1)  % subscript 1 stays a subscript

$\cont*{\Phi_{\mu\nu}}$ % \Phi_{\mu\nu}^\dagger \Phi^{\mu\nu}

\end{document}
Werner
  • 603,163
  • Thanks, but unfortunately, that's not what I wanted. I've added more examples to make things clearer. Your version gives me what looks like F_{\mu}\nu F^{\mu}\nu for \cont{F_{\mu\nu}} and a bunch of NoValue for \cont*{\phi}. – SU3 Nov 05 '19 at 04:12
  • @SU3: Yes. I'm expecting a _ in the input, since you didn't provide anything else. You should be more clear in your question presentation and include all possibly inputs. – Werner Nov 05 '19 at 05:03
  • @SU3: I've updated my answer. – Werner Nov 05 '19 at 05:24
  • I'll give your updated solution a try. But in the mean time I've figured out how to do this with xparse + l3regex. – SU3 Nov 05 '19 at 05:28
3

I don't think you should put emphasis on \mu: the summation index can be anything.

Just typeset twice the argument, but changing the first _ into ^.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\cont}{sm}
 {
  \tl_set:Nn \l_suthree_cont_tl { #2 }
  \regex_replace_once:nnN { \_ } { \cU\^ } \l_suthree_cont_tl
  #2 \IfBooleanT{#1}{\sp{\dagger}} \tl_use:N \l_suthree_cont_tl
 }

\tl_new:N \l_suthree_cont_tl

\ExplSyntaxOff

\begin{document}

$\cont{A_\mu}$  $A_\mu A^\mu$

$\cont{F_{\mu\nu}}$  $F_{\mu\nu} F^{\mu\nu}$

$\cont*{\phi}$  $\phi^\dagger \phi$

$\cont*{(D_\mu\phi_1)}$ $(D_\mu\phi_1)^\dagger (D^\mu\phi_1)$

$\cont*{\Phi_{\mu\nu}}$ $\Phi_{\mu\nu}^\dagger \Phi^{\mu\nu}$

\end{document}

enter image description here

I'd not pursue this idea, though. Since the thing you are using is neither a subscript nor a superscript, but both, I'd use a different character to denote it.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\cont}{sm}
 {
  \tl_set:Nn \l_suthree_cont_sub_tl { #2 }
  \tl_set_eq:NN \l_suthree_cont_sup_tl \l_suthree_cont_sub_tl
  \regex_replace_once:nnN { \? } { \cD\_ } \l_suthree_cont_sub_tl
  \regex_replace_once:nnN { \? } { \cU\^ } \l_suthree_cont_sup_tl
  \tl_use:N \l_suthree_cont_sub_tl
  \IfBooleanT{#1}{\sp{\dagger}}
  \tl_use:N \l_suthree_cont_sup_tl
 }

\tl_new:N \l_suthree_cont_sub_tl
\tl_new:N \l_suthree_cont_sup_tl

\ExplSyntaxOff

\begin{document}

$\cont{A?\mu}$  $A_\mu A^\mu$

$\cont{F?{\mu\nu}}$  $F_{\mu\nu} F^{\mu\nu}$

$\cont*{\phi}$  $\phi^\dagger \phi$

$\cont*{\phi_1}$  $\phi_1^\dagger \phi_1$

$\cont*{(D?\mu\phi_1)}$ $(D_\mu\phi_1)^\dagger (D^\mu\phi_1)$

$\cont*{\Phi?{\mu\nu}}$ $\Phi_{\mu\nu}^\dagger \Phi^{\mu\nu}$

\end{document}

enter image description here

egreg
  • 1,121,712
  • You are right that Lorentz indices can be indicated by letters other than μ or ν. But your approach is less general in a different way. It assumes that only and always the first subscript carries Lorentz indices. E.g. \cont*{\phi_1} will come out as \phi_1^\dagger \phi^1. And for numeric indices (actually anything other than greek) I'd want \phi_1^\dagger \phi_1. – SU3 Nov 05 '19 at 18:48
  • I've upvoted both answers, because I learned something from both. Sorry if the question was not very straightforward. It can be hard to communicate implicit assumptions. – SU3 Nov 05 '19 at 18:50
  • 1
    @SU3 In this case it would be much easier to denote the summation index or indices with a different character than _, say ?. – egreg Nov 05 '19 at 21:42
  • I like this suggestion. It is more general and reflects the different nature of the index. – SU3 Nov 06 '19 at 19:01
0

Here's a solution using xparse + l3regex.

\ExplSyntaxOn                                                      
\NewDocumentCommand{\cont}{sm}                                     
{                                                                  
  \tl_set:Nn \l_tmpa_tl {#2}                                       
  \regex_replace_all:nnN {\cD_(\cB.?\c{[mn]u})} {\cU^\1} \l_tmpa_tl
  {#2} \IfBooleanT{#1}{^\dagger} \tl_use:N \l_tmpa_tl              
}                                                                  
\ExplSyntaxOff                                                     
SU3
  • 519