jupyter nbconver如何导出latex

Fri, Jul 12, 2024heziahLatex

执行以下命令,安装需要的包

pip install nbconvert

tlmgr update --self

tlmgr install tcolorbox parskip graphicx caption float xcolor enumerate geometry amsmath amssymb textcomp upquote eurosym fontenc alphabeta ucs inputenc fontspec unicode-math fancyvrb grffile adjustbox hyperref titling longtable booktabs array calc enumitem ulem soul mathrsfs

jupyter nbconvert --to pdf main.ipynb

classic 模板

https://github.com/t-makaro/nb_pdf_template/tree/master/nb_pdf_template/templates

替换 "C:\tools\miniconda3\envs\matlab\share\jupyter\nbconvert\templates\latex\index.tex.j2"

latex
((=- Default to the notebook output style -=))
((* if not cell_style is defined *))
    ((*- set cell_style = 'style_jupyter.tex.j2' -*))
((* endif *))

((=- Inherit from the specified cell style. -=))
((* extends cell_style *))


%===============================================================================
% Latex Article
%===============================================================================

((* block docclass *))
\documentclass[11pt]{article}
((*- endblock docclass -*))

((*- block packages -*))
    \usepackage{parskip}
    \setcounter{secnumdepth}{0} %Suppress section numbers
    ((( super() )))
((*- endblock packages -*))

((*- block definitions -*))
    ((( super() )))
    %Updated MathJax Compatibility (if future behaviour of the notebook changes this may be removed)
    \renewcommand{\TeX}{\ifmmode \textrm{\Oldtex} \else \textbackslash TeX \fi}
    \renewcommand{\LaTeX}{\ifmmode \Oldlatex \else \textbackslash LaTeX \fi}
    
    ((* block repygments *))
    %Reconfigured pygments
    \makeatletter
    \expandafter\def\csname PY@tok@mi\endcsname{\def\PY@tc##1{\textcolor[HTML]{008800}{##1}}} %numbers
    \expandafter\def\csname PY@tok@mf\endcsname{\def\PY@tc##1{\textcolor[HTML]{008800}{##1}}} %numbers
    \expandafter\def\csname PY@tok@nn\endcsname{\def\PY@tc##1{\textcolor[HTML]{000000}{##1}}} %imports
    \expandafter\def\csname PY@tok@ow\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[HTML]{008000}{##1}}} %operator.word
    \expandafter\def\csname PY@tok@o\endcsname{\def\PY@tc##1{\textcolor[HTML]{AA22FF}{\codetrue##1\codefalse}}} %operator
    \makeatother

    \makeatletter
    \newcommand*\@iflatexlater{\@ifl@t@r\fmtversion}
    \@iflatexlater{2016/03/01}{
	    \newcommand{\wordboundary}{4095}}{
	    \newcommand{\wordboundary}{255}}
    \makeatother

    \newif\ifcode
    \codefalse
    \definecolor{Grey}{rgb}{0.40,0.40,0.40}
    %If using XeLaTeX, use magic to not highlight . operators with purple.
    \ifdefined\XeTeXcharclass
    \XeTeXinterchartokenstate = 1
    \newXeTeXintercharclass \mycharclassGrey
    \XeTeXcharclass `. \mycharclassGrey
    \XeTeXinterchartoks 0 \mycharclassGrey   = {\bgroup\ifcode\color{Grey}\else\fi}

    \XeTeXinterchartoks \wordboundary \mycharclassGrey = {\bgroup\ifcode\color{Grey}\else\fi}

    \XeTeXinterchartoks \mycharclassGrey 0   = {\egroup}
    \XeTeXinterchartoks \mycharclassGrey \wordboundary = {\egroup}
    \fi %end magical operator highlighting
    %End Reconfigured Pygments
    ((* endblock repygments *))

    % Header Adjustments
    \renewcommand{\paragraph}{\textbf}
    \renewcommand{\subparagraph}[1]{\textit{\textbf{#1}}}
((*- endblock definitions -*))

((*- block margins -*))
    \geometry{verbose,tmargin=.5in,bmargin=.7in,lmargin=.7in,rmargin=.7in}
((*- endblock margins -*))

((* block maketitle *))((* endblock maketitle *))

((* set charlim = 89 *))

image