Macro only to be defined in math modeWhen to use LetLtxMacro?How do I make a macro require math mode?“bar...
Would these multi-classing house rules cause unintended problems?
How would a Dictatorship make a country more successful?
Why avoid shared user accounts?
We are very unlucky in my court
Cryptic with missing capitals
Parsing a string of key-value pairs as a dictionary
Why Normality assumption in linear regression
Can we use the stored gravitational potential energy of a building to produce power?
What makes the Forgotten Realms "forgotten"?
It took me a lot of time to make this, pls like. (YouTube Comments #1)
Quenching swords in dragon blood; why?
Why is "points exist" not an axiom in geometry?
Disable the ">" operator in Rstudio linux terminal
Can a person refuse a presidential pardon?
Slow moving projectiles from a hand-held weapon - how do they reach the target?
Why does a metal block make a shrill sound but not a wooden block upon hammering?
Word or phrase for showing great skill at something without formal training in it
How would one buy a used TIE Fighter or X-Wing?
What does Cypher mean when he says Neo is "gonna pop"?
How to explain planetary rings pulsating?
Does Improved Divine Smite trigger when a paladin makes an unarmed strike?
What to do when being responsible for data protection in your lab, yet advice is ignored?
How to deal with an incendiary email that was recalled
A minimum of two personnel "are" or "is"?
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
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
add a comment |
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
2
You can just use#1
for the macro:newcommand{mathdef}[2]{% PushPostHook{math}{def#1{#2}}% PushPostHook{display}{def#1{#2}}}
and then usemathdef{d}{mathrm{d}}
, it is not required to usecsname
here.
– Martin Scharrer♦
15 hours ago
1
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 examplevec v
orvec{v}
are immediately clear to yourself and possibly others, while forv
you need to remember or look up the definition somewhere - and it is not that much extra typing.
– Marijn
15 hours ago
add a comment |
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
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
math-mode macros
asked 16 hours ago
Elmar ZanderElmar Zander
1,2001215
1,2001215
2
You can just use#1
for the macro:newcommand{mathdef}[2]{% PushPostHook{math}{def#1{#2}}% PushPostHook{display}{def#1{#2}}}
and then usemathdef{d}{mathrm{d}}
, it is not required to usecsname
here.
– Martin Scharrer♦
15 hours ago
1
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 examplevec v
orvec{v}
are immediately clear to yourself and possibly others, while forv
you need to remember or look up the definition somewhere - and it is not that much extra typing.
– Marijn
15 hours ago
add a comment |
2
You can just use#1
for the macro:newcommand{mathdef}[2]{% PushPostHook{math}{def#1{#2}}% PushPostHook{display}{def#1{#2}}}
and then usemathdef{d}{mathrm{d}}
, it is not required to usecsname
here.
– Martin Scharrer♦
15 hours ago
1
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 examplevec v
orvec{v}
are immediately clear to yourself and possibly others, while forv
you need to remember or look up the definition somewhere - and it is not that much extra typing.
– Marijn
15 hours 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♦
15 hours 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♦
15 hours ago
1
1
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
15 hours 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
15 hours ago
add a comment |
2 Answers
2
active
oldest
votes
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}
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}
add a comment |
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:
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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}
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}
add a comment |
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}
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}
add a comment |
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}
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}
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}
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}
edited 15 hours ago
answered 15 hours ago
egregegreg
723k8819173220
723k8819173220
add a comment |
add a comment |
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:
add a comment |
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:
add a comment |
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:
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:
answered 15 hours ago
Sergei GolovanSergei Golovan
4,2351615
4,2351615
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
You can just use
#1
for the macro:newcommand{mathdef}[2]{% PushPostHook{math}{def#1{#2}}% PushPostHook{display}{def#1{#2}}}
and then usemathdef{d}{mathrm{d}}
, it is not required to usecsname
here.– Martin Scharrer♦
15 hours ago
1
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 examplevec v
orvec{v}
are immediately clear to yourself and possibly others, while forv
you need to remember or look up the definition somewhere - and it is not that much extra typing.– Marijn
15 hours ago