Macro only to be defined in math modeHow do I make a macro require math mode?“bar allowed only in math...

Why are the books in the Game of Thrones citadel library shelved spine inwards?

Propeller Fan - Array Modifier

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

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

Why did the villain in the first Men in Black movie care about Earth's Cockroaches?

Why do neural networks need so many training examples to perform?

How can my powered armor quickly replace its ceramic plates?

What are "industrial chops"?

Which password policy is more secure: one password of length 9 vs. two passwords each of length 8?

How would an AI self awareness kill switch work?

Why would space fleets be aligned?

What is the wife of a henpecked husband called?

How to deal with an incendiary email that was recalled

Caruana vs Carlsen game 10 (WCC) why not 18...Nxb6?

How should I handle players who ignore the session zero agreement?

How to Write Multiple Data Frames in an Excel Sheet

How can I deliver in-universe written lore to players without it being dry exposition?

Why do members of Congress in committee hearings ask witnesses the same question multiple times?

What is the purpose of easy combat scenarios that don't need resource expenditure?

Why do stocks necessarily drop during a recession?

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

Highly technological aliens land nuclear fusion powered ships in medieval city and slaughter everyone, using swords?

Porting Linux to another platform requirements

Program that converts a number to a letter of the alphabet



Macro only to be defined in math mode


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













0















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



























    0















    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

























      0












      0








      0








      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














      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












      share










      share



      share










      asked 2 mins ago









      Elmar ZanderElmar Zander

      1,1751215




      1,1751215






















          0






          active

          oldest

          votes











          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

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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

          IEEEtran - How to include ORCID in TeX/PDF with PdfLatexIs there a standard way to include ORCID in TeX /...

          Cicindela nigrior Przypisy | Menu nawigacyjneCicindela varians unicolorManual for the Identification of the...

          Glossaries-extra: Adding glossaries package to “Clas­sicTh­e­sis” template by Dr. André Miede v. 4.6 ...