Macro only to be defined in math modeWhen to use LetLtxMacro?How do I make a macro require math mode?“bar...

What to do when being responsible for data protection in your lab, yet advice is ignored?

Can I write a book of my D&D game?

How to prevent users from executing commands through browser URL

Why isn't there a non-conducting core wire for high-frequency coil applications

How do you funnel food off a cutting board?

How much mayhem could I cause as a sentient fish?

Roman Numerals equation 1

Can we use the stored gravitational potential energy of a building to produce power?

Why Normality assumption in linear regression

How can I install sudo without using su?

Broken patches on a road

How to deal with an incendiary email that was recalled

Can I become debt free or should I file for bankruptcy? How do I manage my debt and finances?

Error in a formula field

Who is this Ant Woman character in this image alongside the Wasp?

Is it a fallacy if someone claims they need an explanation for every word of your argument to the point where they don't understand common terms?

What is the most triangles you can make from a capital "H" and 3 straight lines?

My cat mixes up the floors in my building. How can I help him?

Why would space fleets be aligned?

Is every normal subgroup the kernel of some self-homomorphism?

how to acknowledge an embarrasing job interview, now that I work directly with the interviewer?

How can animals be objects of ethics without being subjects as well?

Why exactly do action photographers need high fps burst cameras?

Using only 1s, make 29 with the minimum number of digits



Macro only to be defined in math mode


When to use LetLtxMacro?How do I make a macro require math mode?“bar allowed only in math mode” errorPreventing math mode from parsing macro input as math?Math mode macro in TeXstudioHow can I make every occurrence of `+` and `-` be replaced by a macro, but only in math mode?macros and math modeHow to make a math mode non-Ord macro?Behaviour changes in math mode when macro is defined dynamicallyRemove math mode in macro defined textStrange error defining a macro with arguments for math mode













4















I often use short macros in mathematical formulas for frequently used symbols, e.g. d x for a differential dx or, say, v if I need a vector v very often in my text to make things more readable.
However, I get a clash with predefined macros because d stands for a dot under the next character and v for the hacek accent, which I don't want to override (maybe I need them in the bibliography...).



So, I came up with the following to override those macros only in math mode:



documentclass{article}

usepackage{everyhook}
newcommand{mathdef}[2]{%
PushPostHook{math}{expandafterdefcsname #1endcsname{#2}}%
PushPostHook{display}{expandafterdefcsname #1endcsname{#2}}}
mathdef{d}{mathrm{d}}%

begin{document}
d x is an x with a dot below and $int f(x) d x$ is an integral over $x$.
end{document}


However, I would like the command to be defined like normal macros too, i.e. mathdef{d}{mathrm{d}}, and also be able to take arguments, but all my experiments with expandafter, unexpanded, etc. didn't work out and led only to the usual strange error messages. Any hint how I can do that?



Furthermore, do you think there is a big performance penalty using everymath like that in a large document?










share|improve this question


















  • 2





    You can just use #1 for the macro: newcommand{mathdef}[2]{% PushPostHook{math}{def#1{#2}}% PushPostHook{display}{def#1{#2}}} and then use mathdef{d}{mathrm{d}}, it is not required to use csname here.

    – Martin Scharrer
    1 hour ago











  • Of course the easiest way to avoid problems is to choose different macro names, e.g., md for a differential. Or, especially for short substitutions, don't use macros at all, because they make the text less readable, for example vec v or vec{v} are immediately clear to yourself and possibly others, while for v you need to remember or look up the definition somewhere - and it is not that much extra typing.

    – Marijn
    56 mins ago
















4















I often use short macros in mathematical formulas for frequently used symbols, e.g. d x for a differential dx or, say, v if I need a vector v very often in my text to make things more readable.
However, I get a clash with predefined macros because d stands for a dot under the next character and v for the hacek accent, which I don't want to override (maybe I need them in the bibliography...).



So, I came up with the following to override those macros only in math mode:



documentclass{article}

usepackage{everyhook}
newcommand{mathdef}[2]{%
PushPostHook{math}{expandafterdefcsname #1endcsname{#2}}%
PushPostHook{display}{expandafterdefcsname #1endcsname{#2}}}
mathdef{d}{mathrm{d}}%

begin{document}
d x is an x with a dot below and $int f(x) d x$ is an integral over $x$.
end{document}


However, I would like the command to be defined like normal macros too, i.e. mathdef{d}{mathrm{d}}, and also be able to take arguments, but all my experiments with expandafter, unexpanded, etc. didn't work out and led only to the usual strange error messages. Any hint how I can do that?



Furthermore, do you think there is a big performance penalty using everymath like that in a large document?










share|improve this question


















  • 2





    You can just use #1 for the macro: newcommand{mathdef}[2]{% PushPostHook{math}{def#1{#2}}% PushPostHook{display}{def#1{#2}}} and then use mathdef{d}{mathrm{d}}, it is not required to use csname here.

    – Martin Scharrer
    1 hour ago











  • Of course the easiest way to avoid problems is to choose different macro names, e.g., md for a differential. Or, especially for short substitutions, don't use macros at all, because they make the text less readable, for example vec v or vec{v} are immediately clear to yourself and possibly others, while for v you need to remember or look up the definition somewhere - and it is not that much extra typing.

    – Marijn
    56 mins ago














4












4








4








I often use short macros in mathematical formulas for frequently used symbols, e.g. d x for a differential dx or, say, v if I need a vector v very often in my text to make things more readable.
However, I get a clash with predefined macros because d stands for a dot under the next character and v for the hacek accent, which I don't want to override (maybe I need them in the bibliography...).



So, I came up with the following to override those macros only in math mode:



documentclass{article}

usepackage{everyhook}
newcommand{mathdef}[2]{%
PushPostHook{math}{expandafterdefcsname #1endcsname{#2}}%
PushPostHook{display}{expandafterdefcsname #1endcsname{#2}}}
mathdef{d}{mathrm{d}}%

begin{document}
d x is an x with a dot below and $int f(x) d x$ is an integral over $x$.
end{document}


However, I would like the command to be defined like normal macros too, i.e. mathdef{d}{mathrm{d}}, and also be able to take arguments, but all my experiments with expandafter, unexpanded, etc. didn't work out and led only to the usual strange error messages. Any hint how I can do that?



Furthermore, do you think there is a big performance penalty using everymath like that in a large document?










share|improve this question














I often use short macros in mathematical formulas for frequently used symbols, e.g. d x for a differential dx or, say, v if I need a vector v very often in my text to make things more readable.
However, I get a clash with predefined macros because d stands for a dot under the next character and v for the hacek accent, which I don't want to override (maybe I need them in the bibliography...).



So, I came up with the following to override those macros only in math mode:



documentclass{article}

usepackage{everyhook}
newcommand{mathdef}[2]{%
PushPostHook{math}{expandafterdefcsname #1endcsname{#2}}%
PushPostHook{display}{expandafterdefcsname #1endcsname{#2}}}
mathdef{d}{mathrm{d}}%

begin{document}
d x is an x with a dot below and $int f(x) d x$ is an integral over $x$.
end{document}


However, I would like the command to be defined like normal macros too, i.e. mathdef{d}{mathrm{d}}, and also be able to take arguments, but all my experiments with expandafter, unexpanded, etc. didn't work out and led only to the usual strange error messages. Any hint how I can do that?



Furthermore, do you think there is a big performance penalty using everymath like that in a large document?







math-mode macros






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 1 hour ago









Elmar ZanderElmar Zander

1,1951215




1,1951215








  • 2





    You can just use #1 for the macro: newcommand{mathdef}[2]{% PushPostHook{math}{def#1{#2}}% PushPostHook{display}{def#1{#2}}} and then use mathdef{d}{mathrm{d}}, it is not required to use csname here.

    – Martin Scharrer
    1 hour ago











  • Of course the easiest way to avoid problems is to choose different macro names, e.g., md for a differential. Or, especially for short substitutions, don't use macros at all, because they make the text less readable, for example vec v or vec{v} are immediately clear to yourself and possibly others, while for v you need to remember or look up the definition somewhere - and it is not that much extra typing.

    – Marijn
    56 mins ago














  • 2





    You can just use #1 for the macro: newcommand{mathdef}[2]{% PushPostHook{math}{def#1{#2}}% PushPostHook{display}{def#1{#2}}} and then use mathdef{d}{mathrm{d}}, it is not required to use csname here.

    – Martin Scharrer
    1 hour ago











  • Of course the easiest way to avoid problems is to choose different macro names, e.g., md for a differential. Or, especially for short substitutions, don't use macros at all, because they make the text less readable, for example vec v or vec{v} are immediately clear to yourself and possibly others, while for v you need to remember or look up the definition somewhere - and it is not that much extra typing.

    – Marijn
    56 mins ago








2




2





You can just use #1 for the macro: newcommand{mathdef}[2]{% PushPostHook{math}{def#1{#2}}% PushPostHook{display}{def#1{#2}}} and then use mathdef{d}{mathrm{d}}, it is not required to use csname here.

– Martin Scharrer
1 hour ago





You can just use #1 for the macro: newcommand{mathdef}[2]{% PushPostHook{math}{def#1{#2}}% PushPostHook{display}{def#1{#2}}} and then use mathdef{d}{mathrm{d}}, it is not required to use csname here.

– Martin Scharrer
1 hour ago













Of course the easiest way to avoid problems is to choose different macro names, e.g., md for a differential. Or, especially for short substitutions, don't use macros at all, because they make the text less readable, for example vec v or vec{v} are immediately clear to yourself and possibly others, while for v you need to remember or look up the definition somewhere - and it is not that much extra typing.

– Marijn
56 mins ago





Of course the easiest way to avoid problems is to choose different macro names, e.g., md for a differential. Or, especially for short substitutions, don't use macros at all, because they make the text less readable, for example vec v or vec{v} are immediately clear to yourself and possibly others, while for v you need to remember or look up the definition somewhere - and it is not that much extra typing.

– Marijn
56 mins ago










2 Answers
2






active

oldest

votes


















2














I would not use everymath.



documentclass{article}
usepackage{letltxmacro}

makeatletter
newcommand{mathdef}[2]{%
@ifundefined{#1}{@mathdef@new{#1}{#2}}{@mathdef@remember{#1}{#2}}
}

newcommand{@mathdef@remember}[2]{%
expandafterLetLtxMacro
csname textmode@#1expandafterendcsname
csname #1endcsname
expandafterdefcsname mathmode@#1endcsname{#2}%
expandafterDeclareRobustCommandcsname#1endcsname{%
ifmmodeexpandafter@firstoftwoelseexpandafter@secondoftwofi
{csname mathmode@#1endcsname}{csname textmode@#1endcsname}%
}%
}
newcommand{@mathdef@new}[2]{%
expandafterDeclareRobustCommandcsname#1endcsname{#2}%
}
makeatother

mathdef{d}{mathop{}!mathrm{d}}

begin{document}

d{x} is an x with a dot below and $int f(x) d x$ is an integral over $x$.

end{document}


enter image description here



I don't think this is a good way to go, though. It's confusing and prone to errors.



See When to use LetLtxMacro? for information about LetLtxMacro.



It's a bit easier with etoolbox:



documentclass{article}
usepackage{etoolbox}

makeatletter
newcommand{mathdef}[2]{%
@ifundefined{#1}{@mathdef@new{#1}{#2}}{@mathdef@remember{#1}{#2}}
}

newcommand{@mathdef@remember}[2]{%
expandafterrobustifycsname#1endcsname
csletcs{textmode@#1}{#1}%
csdef{mathmode@#1}{#2}%
protectedcsdef{#1}{%
ifmmodeexpandafter@firstoftwoelseexpandafter@secondoftwofi
{csuse{mathmode@#1}}{csuse{textmode@#1}}%
}%
}
newcommand{@mathdef@new}[2]{%
protectedcsdef{#1}{#2}%
}
makeatother

mathdef{d}{mathop{}!mathrm{d}}

begin{document}

d{x} is an x with a dot below and $int f(x) d x$ is an integral over $x$.

end{document}





share|improve this answer

































    1














    My idea is very similar to egreg's, but I'd like to add an optional argument, so the math command could process arguments itself. The code:



    documentclass{article}

    usepackage{xparse}
    DeclareDocumentCommand{mathdef}{mO{0}m}{%
    expandafterletcsname oldstring#1endcsname=#1
    expandafternewcommandcsname newstring#1endcsname[#2]{#3}
    renewcommand#1{%
    ifmmode
    expandafterletexpandafternextcsname newstring#1endcsname
    else
    expandafterletexpandafternextcsname oldstring#1endcsname
    fi
    next
    }%
    }

    mathdef{v}[1]{tilde{#1}}
    mathdef{d}{mathrm{d}}

    begin{document}
    Hav{c}ek and tilde $v{a}+v{b}=1$.

    d x is an x with a dot below and $int f(x) d x$ is an integral over $x$.
    end{document}


    The result:



    enter image description here






    share|improve this answer























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "85"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f477280%2fmacro-only-to-be-defined-in-math-mode%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      I would not use everymath.



      documentclass{article}
      usepackage{letltxmacro}

      makeatletter
      newcommand{mathdef}[2]{%
      @ifundefined{#1}{@mathdef@new{#1}{#2}}{@mathdef@remember{#1}{#2}}
      }

      newcommand{@mathdef@remember}[2]{%
      expandafterLetLtxMacro
      csname textmode@#1expandafterendcsname
      csname #1endcsname
      expandafterdefcsname mathmode@#1endcsname{#2}%
      expandafterDeclareRobustCommandcsname#1endcsname{%
      ifmmodeexpandafter@firstoftwoelseexpandafter@secondoftwofi
      {csname mathmode@#1endcsname}{csname textmode@#1endcsname}%
      }%
      }
      newcommand{@mathdef@new}[2]{%
      expandafterDeclareRobustCommandcsname#1endcsname{#2}%
      }
      makeatother

      mathdef{d}{mathop{}!mathrm{d}}

      begin{document}

      d{x} is an x with a dot below and $int f(x) d x$ is an integral over $x$.

      end{document}


      enter image description here



      I don't think this is a good way to go, though. It's confusing and prone to errors.



      See When to use LetLtxMacro? for information about LetLtxMacro.



      It's a bit easier with etoolbox:



      documentclass{article}
      usepackage{etoolbox}

      makeatletter
      newcommand{mathdef}[2]{%
      @ifundefined{#1}{@mathdef@new{#1}{#2}}{@mathdef@remember{#1}{#2}}
      }

      newcommand{@mathdef@remember}[2]{%
      expandafterrobustifycsname#1endcsname
      csletcs{textmode@#1}{#1}%
      csdef{mathmode@#1}{#2}%
      protectedcsdef{#1}{%
      ifmmodeexpandafter@firstoftwoelseexpandafter@secondoftwofi
      {csuse{mathmode@#1}}{csuse{textmode@#1}}%
      }%
      }
      newcommand{@mathdef@new}[2]{%
      protectedcsdef{#1}{#2}%
      }
      makeatother

      mathdef{d}{mathop{}!mathrm{d}}

      begin{document}

      d{x} is an x with a dot below and $int f(x) d x$ is an integral over $x$.

      end{document}





      share|improve this answer






























        2














        I would not use everymath.



        documentclass{article}
        usepackage{letltxmacro}

        makeatletter
        newcommand{mathdef}[2]{%
        @ifundefined{#1}{@mathdef@new{#1}{#2}}{@mathdef@remember{#1}{#2}}
        }

        newcommand{@mathdef@remember}[2]{%
        expandafterLetLtxMacro
        csname textmode@#1expandafterendcsname
        csname #1endcsname
        expandafterdefcsname mathmode@#1endcsname{#2}%
        expandafterDeclareRobustCommandcsname#1endcsname{%
        ifmmodeexpandafter@firstoftwoelseexpandafter@secondoftwofi
        {csname mathmode@#1endcsname}{csname textmode@#1endcsname}%
        }%
        }
        newcommand{@mathdef@new}[2]{%
        expandafterDeclareRobustCommandcsname#1endcsname{#2}%
        }
        makeatother

        mathdef{d}{mathop{}!mathrm{d}}

        begin{document}

        d{x} is an x with a dot below and $int f(x) d x$ is an integral over $x$.

        end{document}


        enter image description here



        I don't think this is a good way to go, though. It's confusing and prone to errors.



        See When to use LetLtxMacro? for information about LetLtxMacro.



        It's a bit easier with etoolbox:



        documentclass{article}
        usepackage{etoolbox}

        makeatletter
        newcommand{mathdef}[2]{%
        @ifundefined{#1}{@mathdef@new{#1}{#2}}{@mathdef@remember{#1}{#2}}
        }

        newcommand{@mathdef@remember}[2]{%
        expandafterrobustifycsname#1endcsname
        csletcs{textmode@#1}{#1}%
        csdef{mathmode@#1}{#2}%
        protectedcsdef{#1}{%
        ifmmodeexpandafter@firstoftwoelseexpandafter@secondoftwofi
        {csuse{mathmode@#1}}{csuse{textmode@#1}}%
        }%
        }
        newcommand{@mathdef@new}[2]{%
        protectedcsdef{#1}{#2}%
        }
        makeatother

        mathdef{d}{mathop{}!mathrm{d}}

        begin{document}

        d{x} is an x with a dot below and $int f(x) d x$ is an integral over $x$.

        end{document}





        share|improve this answer




























          2












          2








          2







          I would not use everymath.



          documentclass{article}
          usepackage{letltxmacro}

          makeatletter
          newcommand{mathdef}[2]{%
          @ifundefined{#1}{@mathdef@new{#1}{#2}}{@mathdef@remember{#1}{#2}}
          }

          newcommand{@mathdef@remember}[2]{%
          expandafterLetLtxMacro
          csname textmode@#1expandafterendcsname
          csname #1endcsname
          expandafterdefcsname mathmode@#1endcsname{#2}%
          expandafterDeclareRobustCommandcsname#1endcsname{%
          ifmmodeexpandafter@firstoftwoelseexpandafter@secondoftwofi
          {csname mathmode@#1endcsname}{csname textmode@#1endcsname}%
          }%
          }
          newcommand{@mathdef@new}[2]{%
          expandafterDeclareRobustCommandcsname#1endcsname{#2}%
          }
          makeatother

          mathdef{d}{mathop{}!mathrm{d}}

          begin{document}

          d{x} is an x with a dot below and $int f(x) d x$ is an integral over $x$.

          end{document}


          enter image description here



          I don't think this is a good way to go, though. It's confusing and prone to errors.



          See When to use LetLtxMacro? for information about LetLtxMacro.



          It's a bit easier with etoolbox:



          documentclass{article}
          usepackage{etoolbox}

          makeatletter
          newcommand{mathdef}[2]{%
          @ifundefined{#1}{@mathdef@new{#1}{#2}}{@mathdef@remember{#1}{#2}}
          }

          newcommand{@mathdef@remember}[2]{%
          expandafterrobustifycsname#1endcsname
          csletcs{textmode@#1}{#1}%
          csdef{mathmode@#1}{#2}%
          protectedcsdef{#1}{%
          ifmmodeexpandafter@firstoftwoelseexpandafter@secondoftwofi
          {csuse{mathmode@#1}}{csuse{textmode@#1}}%
          }%
          }
          newcommand{@mathdef@new}[2]{%
          protectedcsdef{#1}{#2}%
          }
          makeatother

          mathdef{d}{mathop{}!mathrm{d}}

          begin{document}

          d{x} is an x with a dot below and $int f(x) d x$ is an integral over $x$.

          end{document}





          share|improve this answer















          I would not use everymath.



          documentclass{article}
          usepackage{letltxmacro}

          makeatletter
          newcommand{mathdef}[2]{%
          @ifundefined{#1}{@mathdef@new{#1}{#2}}{@mathdef@remember{#1}{#2}}
          }

          newcommand{@mathdef@remember}[2]{%
          expandafterLetLtxMacro
          csname textmode@#1expandafterendcsname
          csname #1endcsname
          expandafterdefcsname mathmode@#1endcsname{#2}%
          expandafterDeclareRobustCommandcsname#1endcsname{%
          ifmmodeexpandafter@firstoftwoelseexpandafter@secondoftwofi
          {csname mathmode@#1endcsname}{csname textmode@#1endcsname}%
          }%
          }
          newcommand{@mathdef@new}[2]{%
          expandafterDeclareRobustCommandcsname#1endcsname{#2}%
          }
          makeatother

          mathdef{d}{mathop{}!mathrm{d}}

          begin{document}

          d{x} is an x with a dot below and $int f(x) d x$ is an integral over $x$.

          end{document}


          enter image description here



          I don't think this is a good way to go, though. It's confusing and prone to errors.



          See When to use LetLtxMacro? for information about LetLtxMacro.



          It's a bit easier with etoolbox:



          documentclass{article}
          usepackage{etoolbox}

          makeatletter
          newcommand{mathdef}[2]{%
          @ifundefined{#1}{@mathdef@new{#1}{#2}}{@mathdef@remember{#1}{#2}}
          }

          newcommand{@mathdef@remember}[2]{%
          expandafterrobustifycsname#1endcsname
          csletcs{textmode@#1}{#1}%
          csdef{mathmode@#1}{#2}%
          protectedcsdef{#1}{%
          ifmmodeexpandafter@firstoftwoelseexpandafter@secondoftwofi
          {csuse{mathmode@#1}}{csuse{textmode@#1}}%
          }%
          }
          newcommand{@mathdef@new}[2]{%
          protectedcsdef{#1}{#2}%
          }
          makeatother

          mathdef{d}{mathop{}!mathrm{d}}

          begin{document}

          d{x} is an x with a dot below and $int f(x) d x$ is an integral over $x$.

          end{document}






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 44 mins ago

























          answered 52 mins ago









          egregegreg

          723k8819163219




          723k8819163219























              1














              My idea is very similar to egreg's, but I'd like to add an optional argument, so the math command could process arguments itself. The code:



              documentclass{article}

              usepackage{xparse}
              DeclareDocumentCommand{mathdef}{mO{0}m}{%
              expandafterletcsname oldstring#1endcsname=#1
              expandafternewcommandcsname newstring#1endcsname[#2]{#3}
              renewcommand#1{%
              ifmmode
              expandafterletexpandafternextcsname newstring#1endcsname
              else
              expandafterletexpandafternextcsname oldstring#1endcsname
              fi
              next
              }%
              }

              mathdef{v}[1]{tilde{#1}}
              mathdef{d}{mathrm{d}}

              begin{document}
              Hav{c}ek and tilde $v{a}+v{b}=1$.

              d x is an x with a dot below and $int f(x) d x$ is an integral over $x$.
              end{document}


              The result:



              enter image description here






              share|improve this answer




























                1














                My idea is very similar to egreg's, but I'd like to add an optional argument, so the math command could process arguments itself. The code:



                documentclass{article}

                usepackage{xparse}
                DeclareDocumentCommand{mathdef}{mO{0}m}{%
                expandafterletcsname oldstring#1endcsname=#1
                expandafternewcommandcsname newstring#1endcsname[#2]{#3}
                renewcommand#1{%
                ifmmode
                expandafterletexpandafternextcsname newstring#1endcsname
                else
                expandafterletexpandafternextcsname oldstring#1endcsname
                fi
                next
                }%
                }

                mathdef{v}[1]{tilde{#1}}
                mathdef{d}{mathrm{d}}

                begin{document}
                Hav{c}ek and tilde $v{a}+v{b}=1$.

                d x is an x with a dot below and $int f(x) d x$ is an integral over $x$.
                end{document}


                The result:



                enter image description here






                share|improve this answer


























                  1












                  1








                  1







                  My idea is very similar to egreg's, but I'd like to add an optional argument, so the math command could process arguments itself. The code:



                  documentclass{article}

                  usepackage{xparse}
                  DeclareDocumentCommand{mathdef}{mO{0}m}{%
                  expandafterletcsname oldstring#1endcsname=#1
                  expandafternewcommandcsname newstring#1endcsname[#2]{#3}
                  renewcommand#1{%
                  ifmmode
                  expandafterletexpandafternextcsname newstring#1endcsname
                  else
                  expandafterletexpandafternextcsname oldstring#1endcsname
                  fi
                  next
                  }%
                  }

                  mathdef{v}[1]{tilde{#1}}
                  mathdef{d}{mathrm{d}}

                  begin{document}
                  Hav{c}ek and tilde $v{a}+v{b}=1$.

                  d x is an x with a dot below and $int f(x) d x$ is an integral over $x$.
                  end{document}


                  The result:



                  enter image description here






                  share|improve this answer













                  My idea is very similar to egreg's, but I'd like to add an optional argument, so the math command could process arguments itself. The code:



                  documentclass{article}

                  usepackage{xparse}
                  DeclareDocumentCommand{mathdef}{mO{0}m}{%
                  expandafterletcsname oldstring#1endcsname=#1
                  expandafternewcommandcsname newstring#1endcsname[#2]{#3}
                  renewcommand#1{%
                  ifmmode
                  expandafterletexpandafternextcsname newstring#1endcsname
                  else
                  expandafterletexpandafternextcsname oldstring#1endcsname
                  fi
                  next
                  }%
                  }

                  mathdef{v}[1]{tilde{#1}}
                  mathdef{d}{mathrm{d}}

                  begin{document}
                  Hav{c}ek and tilde $v{a}+v{b}=1$.

                  d x is an x with a dot below and $int f(x) d x$ is an integral over $x$.
                  end{document}


                  The result:



                  enter image description here







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 41 mins ago









                  Sergei GolovanSergei Golovan

                  4,2251615




                  4,2251615






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f477280%2fmacro-only-to-be-defined-in-math-mode%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Benedict Cumberbatch Contingut Inicis Debut professional Premis Filmografia bàsica Premis i...

                      Monticle de plataforma Contingut Est de Nord Amèrica Interpretacions Altres cultures Vegeu...

                      Escacs Janus Enllaços externs Menú de navegacióEscacs JanusJanusschachBrainKing.comChessV