ãã®è¨äºã¯TeX&LaTeX Advent Calendar 16æ¥ç®ã®è¨äºã§ãï¼
ã¯ããã«
Jupyterã¨ã¯
Jupyterã¨ã¯ãã©ã¦ã¶ä¸ã§èµ·åããREPL(Read-eval-print loop)ã§ãï¼ è¨è¿°ããããã°ã©ã ã¨å®è¡çµæãé次è¨é²ããã¦ããã®ã§ï¼éå»ã®ã³ã¼ããè¦è¿ãããå¤æ´ã§ãã便å©ãªãã¼ã«ã§ãï¼
ç¾å¨ã¯æ§ã ãªè¨èªã«å¯¾å¿ãã¦ãã¾ããï¼å ã ã¯Pythonç¨ã®ãã¼ã«ã ã£ãã¿ããã§ãï¼
ä½ã£ããã®
ä»åã¯LaTeXçJupyterï¼åä»ãã¦JupyTeXãä½ãã¾ããï¼ ããã¯
Jupyterã§LaTeXã使ããããã«ãªããã©ã°ã¤ã³
ã§ã¯ãªãï¼
LaTeXã§å®è£ ããJupyter
ã§ãï¼ éè¦ã¯ããã¾ããï¼
ããããï¼ç§ï¼Jupyter使ã£ã¦ãª(ryï¼
ä½ãæ¹
ä½ãæ¹ã¯ç°¡åï¼
- PythonTeX
- tcolorbox
ãããã°ã§ãã¾ãï¼
PythonTeX
PythonTeXã¯LaTeXã§çµçããææ¸å ã«Pythonã³ã¼ããåãè¾¼ã¿ï¼å®è¡çµæã表示ããããã¨ãã§ããããã±ã¼ã¸ã§ãï¼ TeXLiveã«ã¯ããã©ã«ãã§å ¥ã£ã¦ãã¾ãï¼ åä½ã«ã¯Pygmentsãå¿ è¦ãªã®ã§ã¤ã³ã¹ãã¼ã«ãã¾ãï¼
pip install pygments
ããã§ä½¿ãã¾ãï¼
å®éã«ä½¿ãä¾ã示ãã¾ãï¼ ç¬¬ä¸å¼æ°ãã第äºå¼æ°ã¾ã§ã®å ¨ã¦ã®æ´æ°ã®ç·åã表示ããã½ã¼ã¹ã§ãï¼
\documentclass{article} \usepackage{pythontex} \newcommand{\mysum}[2]{% $\displaystyle\sum_{k=#1}^{#2} k = \py{sum(range(#1, #2+1))}$}% \begin{document} \mysum{1}{100} \end{document}
ã¿ã¤ãã»ããã¯
ã§ã§ãã¾ãï¼é©å®latex 㯠platexãuplatexã«ç½®ãæããã¾ãï¼ ãããªæãã«ã¿ã¤ãã»ããã§ãã¾ãï¼
tcolorbox
Jupyterã¿ãããªè¦ãç®ãä½ãã®ã«ä½¿ãã¾ãï¼æ§ã ãªæ ãä½ããã¨ãã§ããããã±ã¼ã¸ã§ãï¼ æ ç·ï¼æ å ã®è²ä»ãã¯ãã¡ããï¼å½±ãæ§ã ãªã¿ã¤ãã§ã¤ãããï¼ TikZã¨é£æºãã§ããã®ã§ãã¶ã¤ã³ã®èªç±åº¦ã¯é«ãã§ãï¼
ä»åã¯
tcolorboxによる装飾表現(TeXユーザの集い2015)
http://texdoc.net/texmf-dist/doc/latex/tcolorbox/tcolorbox.pdf
ãåèã«æ é¨åã®ã½ã¼ã¹ãæ¸ãã¾ããï¼
å®éã«ä½ã
ãã¶ã¤ã³
å®å ¨ã«REPLã§Jupyterã¿ãããªã·ã¹ãã ã«ããã®ã¯ç¡çã§ãï¼ ãªã®ã§ï¼ã¿ã¤ãã»ããå¾ã®è¦ãç®ãä¼¼ãããã¨ã«ãã¾ããï¼
- TeXãã¡ã¤ã«å ã«pythonã®ã³ã¼ããæ¸ãã
- pythonã®ã³ã¼ãã追å ãã¦ã¿ã¤ãã»ããããã¨ã³ã¼ãã¨å®è¡çµæãã»ããã§æç»ããã
ã¨ãã£ãæãã«ã§ããã¨ããã§ããï¼LaTeXã®ã½ã¼ã¹ã¨ã¿ã¤ãã»ããå¾ã®PDFã®çæ³ã¯ä»¥ä¸ã¿ãããªæãã§ãï¼
\begin{jupytex} print("hello") \nextframe a = 5 b = 10 c = a+b print(c) \nextframe from matplotlib import pyplot as plt import numpy x = numpy.arange(0, 10, 0.1) y = numpy.cos(x) plt.plot(x,y) plt.show() ... \end{jupytex}
ã³ã¼ã&å®è¡çµæ表示ã®ä»çµã¿
ã³ã¼ãã¨å®è¡çµæ表示ã®2ã¤ã§ã³ã¼ãã使ãã®ã§ï¼ã³ã¼ãèªä½ãå¤æ°ãä½ãã«æ ¼ç´ããå¿ è¦ãããã¾ãï¼ ä»åã¯jupytexç°å¢å ã«ã½ã¼ã¹ãæ¸ãã®ã§ï¼ãã¡ã¤ã«ã«ä¿åãã¦èªã¿åºããã¨ã«ãã¾ããï¼ è¨è¿°ããã³ã¼ãããã¡ã¤ã«ã«ä¿åããã®ã«ã¯\VerbatimOutåã³\endVerbatimOutã使ãã¾ãï¼
\VerbatimOut{<ãã¡ã¤ã«å>} ä½ãããã®ããã¹ã \endVerbatimOut
ã§ç¹å®ã®ãã¡ã¤ã«ã«ä¿åãããã¨ãã§ãã¾ãï¼ filecontentsç°å¢ãåæ§ã®ç¨éã§ä½¿ããç°å¢ã§ããï¼ä»åã¯èªèº«ã®ç°å¢å®ç¾©ã®ä¸ã§è¡ãã®ã§ï¼\VerbatimOutã³ãã³ãã使ãã¾ããï¼
ã½ã¼ã¹ã³ã¼ãã®è¡¨ç¤º
ã½ã¼ã¹ã³ã¼ãã®è¡¨ç¤ºã¯ \inputpygmentsã³ãã³ãã§ã§ãã¾ãï¼ã·ã³ã¿ãã¯ã¹ãã¤ã©ã¤ãæ©è½ãããã¾ãï¼
\inputpygments{python}{<ãã¡ã¤ã«å>}
å®è¡çµæã®è¡¨ç¤º
ã¨ã¦ã大å¤ã§ããï¼PythonTeXã¯TeXãã¡ã¤ã«ä¸ã«æ¸ããpythonã®ã³ã¼ããèªãã§å®è¡çµæã®è¡¨ç¤ºã¯ã§ãã¾ããï¼ ç¾å¨ã®ãã¼ã¸ã§ã³ã§ã¯ï¼pyãã¡ã¤ã«ããèªã¿è¾¼ãã§å®è¡çµæãæç»ããã³ãã³ããããã¾ããï¼ ä»åã¯pyãã¡ã¤ã«ãå®è¡ããpythonã®ã³ãã³ããTeXãã¡ã¤ã«ä¸ã«æ¸ãã¦PythonTeXã§å®è¡ãããã¨ã«ãã¾ããï¼ pythonã³ã¼ãä¸ã«ã·ã§ã«ãªã©ãå®è¡ããã«ã¯subprocessããã±ã¼ã¸ã使ãã¾ãï¼
\begin{pycode} import subprocess def run_and_typeset(fname): print(subprocess.check_output(['python', fname]).decode("utf-8")) pytex.add_dependencies(fname) \end{pycode}
ããã§run_and_typeseté¢æ°ãå®è¡ããæã«å¼æ°ã«åã£ãfnameã®å®è¡çµæã表示ããã¾ãï¼ ç¶ãã¦ï¼ãã¡ã¤ã«åãå¼æ°ã«åãpythonã½ã¼ã¹ã®å®è¡çµæã表示ããã³ãã³ãã以ä¸ã®ããã«å®ç¾©ãã¾ãï¼
\edef\runandtypeset#1{\pyc{run_and_typeset("#1")}}
jupytexç°å¢ãå®ç¾©ãã
\newcounter{pyfilenumber} \setcounter{pyfilenumber}{0} \NewDocumentEnvironment{jupytex}{}% {% \edef\currentfile{jupytex\arabic{pyfilenumber}.py}% \VerbatimOut{\currentfile}% }% {% \endVerbatimOut% \inputpygments{python}{\currentfile}% \expandafter\runandtypeset\expandafter{\currentfile}% \refstepcounter{pyfilenumber}% }
NewDocumentEnvironmentã®è¡ããjupytexç°å¢ã®å®ç¾©ã«ãªãã¾ãï¼ ãã®åã®2è¡ã¯pythonã®ã³ã¼ããä¿åãããã¡ã¤ã«åãã¦ãã¼ã¯ã«ããããã®ã«ã¦ã³ã¿ã«ãªãã¾ãï¼ beginããã¨ï¼currentfileã¨ãããã¯ãã«pythonã®ã³ã¼ããä¿åãããã¡ã¤ã«åãå ¥ãï¼\VerbatimOutãå±éããã¾ãï¼ endããã¨ï¼\endVerbatimOutãå±éããï¼ç°å¢å ã«æ¸ããpythonã³ã¼ããcurrentfileã«ä¿åããã¾ãï¼ ãã®å¾ï¼inputpygmentsã³ãã³ãã§ä»ä¿åãããã¡ã¤ã«ãèªã¿åºãï¼ã½ã¼ã¹ã³ã¼ãã表示ãã¾ãï¼ runandtypesetã³ãã³ãã§å®è¡çµæã表示ãã¾ãï¼æå¾ã«refstepcounterã³ãã³ãã§ãã¡ã¤ã«ã®çªå·ãã¤ã³ã¯ãªã¡ã³ããã¾ãï¼
æ ããã¶ã¤ã³ãã
tcolorboxã§æ ãä½ãã¾ãï¼
ä½ãæ ã¯2ã¤ã§ãï¼
- ã½ã¼ã¹ã³ã¼ããå®è¡çµæãä¹ãç½ãå¤æ ï¼outframebox
- ã½ã¼ã¹ã³ã¼ããä¹ãç°è²ã®å æ ï¼inputframebox
- ä½ç½ãå¶å¾¡ããããã®å®è¡çµæãä¹ãéæãªæ ï¼ outputframebox
outframebox
ãã®æ ãä½ã£ã¦ããã¾ãï¼
tcolorboxã¯ãªãªã¸ãã«ã®æ ãå®ç¾©ã§ããnewtcolorboxã¨ããã³ãã³ããããã¾ãï¼ ããã使ã£ã¦outframeboxã³ãã³ããå®ç¾©ãã¾ãï¼
\newtcolorbox[]{outframebox}{% enhanced, % å½±ãä»ããã®ã«å¿ è¦ãªãªãã·ã§ã³ fuzzy halo=1.8pt with black!30, % æ ã®å¨ãã«ãã£ããå½±ãä»ãã breakable=true, % ãã¼ã¸ãè·¨ãã§æ ã表示ãã colback=white, % èæ¯è²ã¯ç½ boxrule=0.1pt, % ãããã«æ ç·ãä»ãã top=0mm, % æ ä¸é¨ã®ä½ç½ãªã bottom=0mm, % %æ ä¸é¨ã®ä½ç½ãªã right=0mm, % æ å³é¨åã®ä½ç½ãªã left=0mm, % æ å·¦é¨åã®ä½ç½ãªã arc=0pt, % æ ã®è§ã¯ä¸¸ã¿ãªã boxsep=10mm, % æ å¨ãå ¨ä½çã«10mmã®ä½ç½ }%
ãããªæãã«ãªãã¾ãï¼
inputframebox
ãã®æ ãä½ã£ã¦ããã¾ãï¼
ã³ã¼ã表示é¨åï¼ãã¨ãå ¥åé¨åã¯ç°è²ã®æ ã®é£ã«In [ ]:ã¨ãã表示ãããã¾ãï¼ ãã®è¡¨ç¤ºã¯tcolorboxã®overlayæ©è½ã¨tcolorboxãæã£ã¦ããã«ã¦ã³ã¿ã使ã£ã¦å®ç¾ãã¾ãï¼
\newtcolorbox[auto counter]{inputframebox}[1][]{% 第ä¸ãªãã·ã§ã³å¼æ°ã¯ã«ã¦ã³ã¿ã®åæå¤ enhanced, tcbox raise base, % In [ n ] ã®è¡¨ç¤ºãåãããçº breakable=true, frame hidden, % æ ç·ãªã top=0mm, bottom=0mm, right=0mm, left=10mm, arc=1pt, boxsep=10mm, overlay={\begin{tcbclipinterior} % overlayæ©è½ä½¿ã \fill[white] % æ ã®å·¦é¨åã In [ n ] ã®ããã«ç¢ºä¿ % TikZã®ãã¼ãå½¢å¼ã§æç» % thetcbcounterã¯inputframeboxãå¼ã°ãã度ã«ã¤ã³ã¯ãªã¡ã³ãããã ([xshift=10mm]frame.south west) rectangle node[text=blue,] {In[\thetcbcounter]:} (frame.north west); \end{tcbclipinterior}}}
ãããªæãã«ãªãã¾ãï¼
outputframebox
ãã®æ ãä½ã£ã¦ããã¾ãï¼
éæã§ããï¼ãã®æ ãéè¦ã§ãï¼å®è¡çµæããã®ã¾ã¾è¡¨ç¤ºããã¨ï¼ åç´æ¹åã«ããã¦In [ n ] ã¨åãä½ç½®ã«å®è¡çµæãæ¥ã¦ãã¾ãããã§ãï¼
\newtcolorbox[]{outputframebox}{ enhanced, % breakableã®ä¸ã«breakableãå ¥ããæã®ãªãã·ã§ã³ã¨breakããä½ç½®ã®èª¿æ´ enforce breakable=true,shrink break goal=15mm, colback=white, frame hidden, top=0mm, bottom=0mm, right=0mm, left=10mm, boxsep=0mm,}
ã§ãããã£ããã®
è¦ãç®ã¨ã·ã¹ãã ãã§ããã®ã§ã¹ã¿ã¤ã«ãã¡ã¤ã«ã«ã¾ã¨ãã¾ãï¼
ããã§ã¯ï¼ãã®ã¹ã¿ã¤ã«ãã¡ã¤ã«ãèªã¿è¾¼ãã§ä½¿ã£ã¦ããã¾ãï¼
\documentclass[uplatex,dvipdfmx]{jsarticle} \usepackage{jupytex} \pagestyle{empty} \begin{document} \begin{outframebox} \begin{jupytex} print(30+50+580+558+578+73) \end{jupytex} \begin{jupytex} print("Hello") \end{jupytex} \begin{jupytex} print("dodododododo") \end{jupytex} \begin{jupytex} for i in range(1,2000): if i%3 == 0 and i%5 == 0: print("FizzBuzz") elif i%3 == 0: print("Fizz") elif i%5 == 0: print("Buzz") else: print(i) \end{jupytex} \end{outframebox} \end{document}
ã¿ã¤ãã»ããããçµæã¯ãããªæãã§ãï¼
å®éã®PDFãGoogle Driveã«ç½®ãã¦ããã¾ãï¼
è¦ãç®ã¯ããã§ããã®ã§ã¯ãªãã§ãããã
ã§ãã¦ããªãã¨ãã
ããã¾ã§ä½ãã¾ã§ãçµæ§å¤§å¤ã ã£ãã§ããï¼ããã§ãã§ãã¦ããªãã¨ãããããããããã¾ãï¼
- nextframeã³ãã³ãã§æ¬¡ã®å ¥åã«ããããã®ã«ã§ãã¦ãªã
- pythonã®ã½ã¼ã¹ã³ã¼ããå ¥åããæã¯ã¤ã³ãã³ããéãããã¨ã¯è¨±ãããªã
- .latexmkrcã®æ¸ãæ¹ãããããªã
- uplatex -> pythontex -> uplatex -> dvipdfmx ãä¸ã ãããªããã°ãªããªã
- ã½ã¼ã¹ã³ã¼ãã®è¡¨ç¤ºé¨åãæ´æ°ãããªã
- ã¿ã¤ãã»ããã®åº¦ã«python-files-hogehogeãã£ã¬ã¯ããªãæ¶ããªããã°ãªããªã
ããããããã¾ããï¼Advent Calendarã«éã«åããªãã£ããæ¬æ«è»¢åãªã®ã§å¦¥åãã¾ããï¼
ãããã«
jupytexç°å¢ã®å®ç¨æ§
LaTeXã§Jupyterãå®è£ ãããã¨ã¯éè¦ããªãã§ããï¼ jupytexç°å¢åä½ã§ããã°ï¼å¦æ ¡ã®ã¬ãã¼ãã¨ãã«ä½¿ããããããã¾ãã*1ï¼ ã¨ããããã§ä¸å¿Githubã®ãªãã¸ããªãªã³ã¯ãè²¼ã£ã¦ããã¾ãï¼
LaTeXãã¥ã¼ã©ã«ãããã¯ï¼
æ¬å½ã¯ï¼ãã£ã¡ã®Advent Calendarã«ãLaTeXã§å®è£ ãããã¥ã¼ã©ã«ãããããè¼ãããã£ãã®ã§ããï¼ã¾ã ã§ãã¦ããªãã®ã§ï¼ãã¡ããè¼ãã¾ããï¼ LaTeXãã¥ã¼ã©ã«ãããã¯
学生 Advent Calendar 2017 - Adventar
ã
Muroran Institute of Technology Advent Calendar 2017 - Adventar
ã«è¼ããããããã¾ããï¼
ã§ããªãã£ããè¼ãã¾ããï¼