minted-mathematica#

../_images/sphx_glr_minted-mathematica.svg
%!TEX program = xelatex
%!TEX options = -shell-escape --file-line-error --synctex=1
%!TEX encoding = UTF-8 Unicode
\documentclass[
	a4paper,xelatex,everyparhook=compat,
	ja=standard,
	oneside,
	textwidth-limit=50,
]{bxjsbook}
\synctex=1
\evensidemargin 0.0in
\oddsidemargin 0.0in

\XeTeXlinebreaklocale "ja"
\XeTeXlinebreakskip=0pt plus 1pt
\XeTeXlinebreakpenalty=0

\usepackage{fontspec}
\usepackage{xcolor}
%\setmonofont{Menlo}
\setmonofont{Cica} % https://github.com/miiton/Cica 
%% mac 環境ならbrewでinstall可能
%% $ brew tap brew tap homebrew/cask-fonts
%% $ brew  install --cask font-cica

\usepackage[def-file=diffcoeff5,spaced=-1]{diffcoeff}%
\usepackage{pgfplots} %% plotter
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest}
\tikzset{>=latex}
\usetikzlibrary{arrows,arrows.meta,patterns,intersections}
\usepackage{steinmetz}
\newcommand*{\equal}{=}
%
\tikzset{->-/.style={decoration={
  markings,
  mark=at position #1 with {\arrow{>}}},postaction={decorate}}}
%

\usepackage{tikz} %% drawer
\usepackage{tikz-3dplot}
\usetikzlibrary{backgrounds,fit,shadows,patterns,decorations.markings,quotes,angles,arrows.meta}
\usepackage{tabularx}
\usepackage{array}
\usepackage{colortbl}
\usepackage[]{minted}
\usemintedstyle{mathematica}
%% minted style でmathematicaを追加するため
%% $ pip install pygments-mathematica 
%% を実行する必要がある(see https://github.com/rsmenon/pygments-mathematicae)
%% 利用可能なstyle一覧は下記コマンドで確認可能
%% pygmentize -L styles
%% もし
%% ! Package minted Error: Pygments style "mathematica" was not found.
%% となる場合はpythonのPATHが通っていないかも.
\usepackage[most,minted]{tcolorbox}
\tcbuselibrary{theorems,listingsutf8}


%%%
% 日本語でminedを使うためにpython pakageのpygmentizeをtexbinにパスを通す必要があるかも?.
% $ sudo ln -s `which pygmentize` /Library/TeX/texbin
%\captionsetup[listing]{name=Source Code}
%\newenvironment{code}{\captionsetup{type=listing}}{}
\definecolor{LightGray}{gray}{0.8}
\definecolor{outcolor}{HTML}{D84315}

% In[] Out[]の微調整
\newlength{\promptwidth}
\setlength{\promptwidth}{50pt}
\newlength{\promptsep}
\setlength{\promptsep}{5.5pt}
\newlength{\promptshift}
\setlength{\promptshift}{5pt}


\newcommand{\prompt}[4]{%
    \makebox[0pt][r]{\texttt{\color{#2}#1[#3]:#4}}\vspace{-\baselineskip}%
}

% NotebookOut 環境の定義
\newcommand{\pinref}[1]{\texttt{\color{outcolor}{In}[\ref{code:In-#1}]}}
\newcommand{\poutref}[1]{\texttt{\color{outcolor}{Out}[\ref{code:Out-#1}]}}
\newcommand{\nbref}[2]{\texttt{\color{outcolor}{#1}[\ref{#2}]}}
\newcommand{\py}[0]{\texttt{python}}
\newcommand{\md}[0]{\texttt{markdown}}
\newcommand{\colab}[0]{\texttt{Colab}}
\newcommand{\run}[0]{\circled{\blacktriangleright}}


\newcounter{NBin}[section] % notebook counter  section毎に1に戻す
\newcounter{NBout}[section] % notebook counter


%\newcommand{\myLine}[2]{\mintinlinebox{#1}{#2}}
% tcolorbox の定義

\NewTotalTCBox{\cde}{m}{
  on line,
  boxsep=0pt,
  left=1.5pt,
  right=1.5pt,
  top=1pt,
  bottom=1pt,
  colframe=gray!20,
  colback=gray!20,
  rounded corners,
  boxrule=0pt,
}{\mintinline{mathematica}{#1}}

\newtcblisting{nbin}[2][]{
    breakable,
    boxrule=.5pt,
    size=fbox,
    before skip = 8pt,    
    left skip = \promptwidth + \promptsep,
    pad at break*=1mm,
    opacityfill=1,
    after={\par\vspace{0pt}\noindent},
    enhanced jigsaw,
    title={\refstepcounter{NBin}\label{#2} \prompt{\footnotesize{In}}{outcolor}{\footnotesize{\theNBin}}{\hspace{\promptsep + \promptshift}}},
    attach title to upper,
    listing only,
    listing engine=minted,
    minted language=mathematica,   % mintedを使用
    minted options={          % mintedのオプション設定
	    breaklines=true,
	    texcomments,
	    escapeinside=??,	% minted内でのLaTeXコマンドを有効にする
	    mathescape=true,
	    numbersep=10pt,
	    xleftmargin=5pt,
	    fontsize=\scriptsize
	    #1,	    
    },
}

\newtcblisting{nbout}[2][]{
    breakable,
    boxrule=.5pt,
    size=fbox,
    parbox=true,
    left skip = \promptwidth + \promptsep,
    pad at break*=1mm,
    opacityfill=1,
    title={\refstepcounter{NBout}\label{#2} \prompt{\footnotesize{Out}}{outcolor}{\footnotesize{\theNBin}}{\hspace{\promptsep + \promptshift}}},
    after={\par\vspace{0pt}\noindent},    
    attach title to upper,
    listing only,
    listing engine=minted,
    minted language=mathematica,   % mintedを使用
    minted options={          % mintedのオプション設定
	    breaklines=true,
	    texcomments,
	    escapeinside=??,	% minted内でのLaTeXコマンドを有効にする
	    mathescape=true,
	    numbersep=10pt,
	    xleftmargin=5pt,
	    fontsize=\scriptsize
	    #1,	    
    },
}

\newtcblisting{nb}[2][]{
    breakable,
    boxrule=.5pt,
    size=fbox,
    parbox=true,    
    before skip = 5pt,
    after skip = 0pt,  
    left skip = \promptwidth + \promptsep,
    pad at break*=1mm,
    opacityfill=1,
    title={\refstepcounter{NBin}\label{#2} \prompt{\footnotesize{In}}{outcolor}{\footnotesize{\theNBin}}{\hspace{\promptsep + \promptshift}}},
	after={\par\vspace{0pt}\noindent\refstepcounter{NBout}},    
%    fonttitle=\linespread{2}\small,
    attach title to upper,
    listing only,
    minted language=mathematica,   % mintedを使用
    minted options={          % mintedのオプション設定
	    breaklines=true,
	    texcomments,
	    escapeinside=??,
	    mathescape=true,	    
	    numbersep=10pt,
	    xleftmargin=5pt,
	    fontsize=\scriptsize,
	    #1,
    },
}


\newtcolorbox{nbfig}[2][]{
    breakable,
    boxrule=.5pt,
    size=fbox,
    after skip = 8pt,
    left skip = \promptwidth + \promptsep,
    pad at break*=1mm,
    opacityfill=1,
    fontupper=\scriptsize,
    phantom={\refstepcounter{NBout}\label{#2}},
%  	after={\!\!},    
    attach title to upper,
    subtitle style={font=\small, colback=white}, % キャプションのスタイル    
    enhanced,
    overlay={
        \node[anchor=south west, font=\normalsize, yshift=-2ex, xshift=-\promptshift] at (frame.north west) {\prompt{\footnotesize{Out}}{outcolor}{\footnotesize{\theNBout}}{\hspace{\promptsep-8pt+\promptshift}}};
    },
}




\begin{document}
\section{セクション}
\begin{nb}[]{}
f[x_] := Exp[x]
\end{nb}
Notebookの出力を単に出力をコピペすると
\begin{nbin}[]{}
Series[f[x], {x, 0, 10}]
\end{nbin}
\begin{nbout}[]{}
SeriesData[x, 0, {1, 1, 
Rational[1, 2], 
Rational[1, 6], 
Rational[1, 24], 
Rational[1, 120]}, 0, 6, 1]
\end{nbout}
となるので
\begin{nbin}[]{}
% //TeXForm
\end{nbin}
\begin{nbout}[]{}
1+x+\frac{x^2}{2}+\frac{x^3}{6}+\frac{x^4}{24}+\frac{x^5}{120}+O\left(x^6\right)
?$1+x+\frac{x^2}{2}+\frac{x^3}{6}+\frac{x^4}{24}+\frac{x^5}{120}+O\left(x^6\right)$?
\end{nbout}
を得てから?$...$?で括ってエスケープすればそれっぽくなる.
\begin{nbin}[]{}
% //TeXForm
\end{nbin}
\begin{nbout}[]{}
?$1+x+\frac{x^2}{2}+\frac{x^3}{6}+\frac{x^4}{24}+\frac{x^5}{120}+O\left(x^6\right)$?
\end{nbout}
非ascii文字(unicode文字)はwolframノートブックから変換するのは結構面倒なので次のような場合は
\begin{nb}[]{}
?\text{$\alpha $[x$\_$] := 4 x}?
\end{nb}
のように評価式を文字列にして\cde{TeXForm}を掛けてやればそれっぽくなる.
\begin{nbin}[]{}
"α[x_] := 4 x" // TeXForm
\end{nbin}
\begin{nbout}[]{}
\text{$\alpha $[x$\_$] := 4 x}
\end{nbout}

\section{新セクション}
図を貼ったりするのは面倒だが,\cde{nbfig}環境内はlistingしていないので普通のTeX環境で記述すれば良い.
\begin{nbin}[]{}
RandomInteger[{0, 100}, {2, 10}] // TableForm
\end{nbin}
\begin{nbfig}[]{}
$
\left(
\begin{array}{cccccccccc}
 37 & 88 & 38 & 78 & 97 & 5 & 97 & 75 & 36 & 62 \\
 2 & 79 & 29 & 81 & 54 & 22 & 57 & 89 & 50 & 42 \\
\end{array}
\right)
$
\end{nbfig}



\end{document}