Skip to content

Commit be18c28

Browse files
committed
DOC: How to define LaTeX macros
1 parent 96ea902 commit be18c28

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

doc/conf.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@
9797
#}
9898

9999
mathjax_config = {
100-
'TeX': {'equationNumbers': {'autoNumber': 'AMS', 'useLabelIds': True}},
100+
'TeX': {
101+
'equationNumbers': {'autoNumber': 'AMS', 'useLabelIds': True},
102+
'extensions': ['newcommand.js', 'begingroup.js'],
103+
},
101104
}
102105

103106
# Additional files needed for generating LaTeX/PDF output:

doc/markdown-cells.ipynb

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,69 @@
8181
"```"
8282
]
8383
},
84+
{
85+
"cell_type": "markdown",
86+
"metadata": {},
87+
"source": [
88+
"### Defining LaTeX Macros\n",
89+
"\n",
90+
"LaTeX users will be familiar with defining new macros by using\n",
91+
"`\\newcommand` or the more low-level `\\def` command .\n",
92+
"\n",
93+
"If you want to use that for both HTML and LaTeX output,\n",
94+
"you have to take some extra care.\n",
95+
"You should make your definitions like this:\n",
96+
"\n",
97+
"```\n",
98+
"<div hidden>\n",
99+
"\n",
100+
"$\\gdef\\vec#1{\\boldsymbol{#1}}$\n",
101+
"\n",
102+
"\\vskip-\\parskip\n",
103+
"\\vskip-\\baselineskip\n",
104+
"\n",
105+
"</div>\n",
106+
"```\n",
107+
"\n",
108+
"<div hidden>\n",
109+
"\n",
110+
"$\\gdef\\vec#1{\\boldsymbol{#1}}$\n",
111+
"\n",
112+
"\\vskip-\\parskip\n",
113+
"\\vskip-\\baselineskip\n",
114+
"\n",
115+
"</div>\n",
116+
"\n",
117+
"This makes sure that the definition itself is not visible in HTML output\n",
118+
"(by using `<div hidden>`).\n",
119+
"In LaTeX output, the definition creates an empty equation in a new paragraph.\n",
120+
"To get rid of the additional vertical spacing, `\\vskip` is used.\n",
121+
"\n",
122+
"Using `\\def` inside math mode is supported in MathJax,\n",
123+
"but in LaTeX, it doesn't have any effect on later math expressions.\n",
124+
"Using `\\gdef` works in both.\n",
125+
"Sadly, `\\gdef` isn't activated by default in MathJax,\n",
126+
"therefore we need some additional configuration in our `conf.py` file:\n",
127+
"\n",
128+
"```python\n",
129+
"mathjax_config = {\n",
130+
" 'TeX': {\n",
131+
" 'extensions': ['newcommand.js', 'begingroup.js'],\n",
132+
" },\n",
133+
"}\n",
134+
"\n",
135+
"```\n",
136+
"\n",
137+
"With the above definition and the `conf.py` setting in place,\n",
138+
"you can use your new macro like this:\n",
139+
"\n",
140+
"```\n",
141+
"$2 \\vec{a} + \\vec{b}$\n",
142+
"```\n",
143+
"\n",
144+
"... which looks like this: $2 \\vec{a} + \\vec{b}$"
145+
]
146+
},
84147
{
85148
"cell_type": "markdown",
86149
"metadata": {},
@@ -100,7 +163,9 @@
100163
"\n",
101164
"```python\n",
102165
"mathjax_config = {\n",
103-
" 'TeX': {'equationNumbers': {'autoNumber': 'AMS', 'useLabelIds': True}},\n",
166+
" 'TeX': {\n",
167+
" 'equationNumbers': {'autoNumber': 'AMS', 'useLabelIds': True},\n",
168+
" },\n",
104169
"}\n",
105170
"```\n",
106171
"\n",
@@ -491,7 +556,7 @@
491556
"name": "python",
492557
"nbconvert_exporter": "python",
493558
"pygments_lexer": "ipython3",
494-
"version": "3.7.1"
559+
"version": "3.7.2+"
495560
}
496561
},
497562
"nbformat": 4,

0 commit comments

Comments
 (0)