jupyter / nbconvert

Jupyter Notebook Conversion
https://nbconvert.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.74k stars 568 forks source link

\newcommand definitions only valid for current math environment #312

Closed magerton closed 8 years ago

magerton commented 8 years ago

In a Julia 0.45 Jupyter Notebook, though LaTeX \newcommand macros are valid for a whole notebook when defined in a math environment, when the document is converted to LaTeX via File > Download as PDF, it looks like the \newcommand is only valid within that specific math environment.

The following MWE will throw an error and report that the third line contains an undefined control sequence.

$$\newcommand{\hi}{hi} \hi $$       
$\newcommand{\hi}{hi} \hi$         
$\hi$

Version information:

ver Microsoft Windows [Version 6.1.7601]

jupyter --version 4.1.0

python --version Python 2.7.11 :: Anaconda 4.0.0 (64-bit)

jupyter nbconvert --version 4.2.0

Julia: Version 0.4.5 (2016-03-18 00:58 UTC) Official http://julialang.org/ release x86_64-w64-mingw32

pandoc --version pandoc 1.17.0.2 Compiled with texmath 0.8.5, highlighting-kate 0.6.2. Syntax highlighting is supported for the following languages: abc, actionscript, ada, agda, apache, asn1, asp, awk, bash, bibtex, boo, c, changelog, clojure, cmake, coffee, coldfusion, commonlisp, cpp, cs, css, curry, d, diff, djangotemplate, dockerfile, dot, doxygen, doxygenlua, dtd, eiffel, elixir, email, erlang, fasm, fortran, fsharp, gcc, glsl, gnuassembler, go, hamlet, haskell, haxe, html, idris, ini, isocpp, java, javadoc, javascript, json, jsp, julia, kotlin, latex, lex, lilypond, literatecurry, literatehaskell, llvm, lua, m4, makefile, mandoc, markdown, mathematica, matlab, maxima, mediawiki, metafont, mips, modelines, modula2, modula3, monobasic, nasm, noweb, objectivec, objectivecpp, ocaml, octave, opencl, pascal, perl, php, pike, postscript, prolog, pure, python, r, relaxng, relaxngcompact, rest, rhtml, roff, ruby, rust, scala, scheme, sci, sed, sgml, sql, sqlmysql, sqlpostgresql, tcl, tcsh, texinfo, verilog, vhdl, xml, xorg, xslt, xul, yacc, yaml, zsh Default user data directory: C:\Users\mja3\AppData\Roaming\pandoc Copyright (C) 2006-2016 John MacFarlane Web: http://pandoc.org This is free software; see the source for copying conditions. There is no warranty, not even for merchantability or fitness for a particular purpose.

mpacer commented 8 years ago

So, this technically isn't a bug. when you delimit characters in LaTeX using $$ $$ or $ $ {or \[ \] \( \) } or even just { } you create an environment and implicitly define their scope.

Note that you used \newcommand twice, which would have erred if they weren't inside their respective environments.

So technically it's the notebook that is behaving improperly. It shouldn't allow it to get outside of individual environment scopes. Also, if it did have global scope, it should raise an error for declaring newcommand when a command already existed.

If you want it to convert properly when exported to LaTeX or pdf; you can include it in a raw nbconvert cell and it will be left untouched in your LaTeX document, and it will be defined and accessible outside of a specific math environment.

magerton commented 8 years ago

Thanks, @michaelpacer. That totally makes sense.