Debian Codegraph @

texmf-dist/doc/latex/pythontex/pythontex_quickstart.tex

raw
1 \documentclass[twocolumn]{article}
2
3 % Engine-specific settings
4 % Detect pdftex/xetex/luatex, and load appropriate font packages.
5 % This is inspired by the approach in the iftex package.
6 % pdftex:
7 \ifx\pdfmatch\undefined
8 \else
9 \usepackage[T1]{fontenc}
10 \usepackage[utf8]{inputenc}
11 \fi
12 % xetex:
13 \ifx\XeTeXinterchartoks\undefined
14 \else
15 \usepackage{fontspec}
16 \defaultfontfeatures{Ligatures=TeX}
17 \fi
18 % luatex:
19 \ifx\directlua\undefined
20 \else
21 \usepackage{fontspec}
22 \fi
23 % End engine-specific settings
24 \usepackage{lmodern}
25
26 \usepackage[margin=1in]{geometry}
27 \usepackage{microtype}
28
29 \usepackage{pythontex}
30
31 \usepackage[svgnames]{xcolor}
32
33 \usepackage{hyperref}
34 \hypersetup{colorlinks=true,urlcolor=Green,linkcolor=blue}
35
36 \newcommand{\pytex}{Python\TeX}
37 \renewcommand*{\thefootnote}{\fnsymbol{footnote}}
38
39 \title{\vspace{-0.6in} \pytex\ Quickstart \\ {\normalsize \href{https://github.com/gpoore/pythontex}{github.com/gpoore/pythontex}}}
40 \author{}
41 \date{}
42
43
44 \begin{document}
45
46
47 \maketitle
48
49
50
51
52 \section*{Installing}
53
54 \pytex\ requires Python 2.7 or 3.2+. When using \pytex\ with LyX, be aware that LyX may try to use its own version of Python; you may need to reconfigure LyX to use other Python installations.
55
56 \pytex\ is included in TeX Live and MiKTeX. It may be installed via the package manager.
57
58 A Python installation script is included with the package. It should be able to install the package in most situations. Depending on the configuration of your system, you may have to run the installation script with administrative priviliges.
59
60 Detailed installation information is available in the main documentation, \texttt{pythontex.pdf}.
61
62 \section*{Compiling}
63
64 Compiling a document that uses \pytex\ involves three steps: run \LaTeX, run \texttt{pythontex.py}, and finally run \LaTeX\ again. You may wish to create a symlink or launching wrapper for \texttt{pythontex.py}, if one was not created during installation. \pytex\ is compatible with the pdfTeX, XeTeX, and LuaTeX engines, so you can use \texttt{latex}, \texttt{pdflatex}, \texttt{xelatex}, or \texttt{lualatex}.
65
66 The last two compile steps are \emph{only} necessary when code needs to be executed or highlighted. Otherwise, the document may be compiled just like a normal \LaTeX\ document; all output is cached.
67
68 \pytex\ is compatible with \texttt{latexmk}. Details for configuring \texttt{latexmk} are provided in the main documentation.
69
70
71
72 \section*{Basic commands}
73
74 \pygment{latex}{\py} returns a string representation of its argument. For example, \pygment{latex}{\py{2 + 4**2}} produces ``\py{2 + 4**2}'', and \pygment{latex}{\py{'ABC'.lower()}} produces ``\py{'ABC'.lower()}''. \pygment{latex}{\py}'s argument can be delimited by curly braces, or by a matched pair of other characters (just like \pygment{latex}{\verb}).
75
76 \pygment{latex}{\pyc} executes code. By default, anything that is printed is automatically included in the document (see \texttt{autoprint}/\texttt{autostdout} in the main documentation). For example, \pygment{latex}{\pyc{var = 2}} \pyc{var = 2} creates a variable, and then its value may be accessed later via \pygment{latex}{\py{var}}: \py{var}.
77
78 \pygment{latex}{\pyb} executes and typesets code. For example, \pygment{latex}{\pyb{var = 2}} typesets \pyb{var = 2} in addition to creating the variable. If anything is printed, it is not automatically included, but can be accessed via \pygment{latex}{\printpythontex} or \pygment{latex}{\stdoutpythontex}.
79
80 \pygment{latex}{\pyv} only typesets code; nothing is executed. For example, \pygment{latex}{\pyv{var = 2}} produces \pyv{var = 2}.
81
82 \pygment{latex}{\pys} performs variable substitution or string interpolation on code. Substitution fields are denoted by \verb|!{...}|; details about escaping are provided in the main documentation. For example, using the pre-existing variable \pyv{var}, \pygment{latex}{\pys{\verb|var = !{var}|}} yields \pys{\verb|var = !{var}|}.
83
84
85 \section*{Basic environments}
86
87 There are \texttt{pycode}, \texttt{pyblock}, \texttt{pyverbatim}, and \texttt{pysub} environments, which are the environment equivalents of \pygment{latex}{\pyc}, \pygment{latex}{\pyb}, \pygment{latex}{\pyv}, and \pygment{latex}{\pys}. For example,
88 \begin{pygments}{text}
89 \begin{pycode}
90 print(r'\begin{center}')
91 print(r'\textit{A message from Python!}')
92 print(r'\end{center}')
93 \end{pycode}
94 \end{pygments}
95 produces
96 \begin{pycode}
97 print(r'\begin{center}')
98 print(r'\textit{A message from Python!}')
99 print(r'\end{center}')
100 \end{pycode}
101
102 The \pygment{latex}{\begin} and \pygment{latex}{\end} of an environment should be on lines by themselves. Code in environments may be indented; see the \texttt{gobble} option in the main documentation for more details.
103
104
105 \section*{More commands/environments}
106
107 All commands and environments described so far have names beginning with \texttt{py}. There are equivalent commands and environments that begin with \texttt{sympy}; these automatically include
108 \begin{pygments}{python}
109 from sympy import *
110 \end{pygments}
111 There are also equivalent commands and environments that begin with \texttt{pylab}; these automatically use matplotlib's \texttt{pylab} module via
112 \begin{pygments}{python}
113 from pylab import *
114 \end{pygments}
115 The \texttt{sympy} and \texttt{pylab} commands and environments execute code in separate sessions from the \texttt{py} commands and environments. This can make it easier to avoid namespace conflicts.
116
117 There is also a \texttt{pyconsole} environment that emulates a Python interactive console. For example,
118 \begin{pygments}{text}
119 \begin{pyconsole}
120 var = 1 + 1
121 var
122 \end{pyconsole}
123 \end{pygments}
124 yields
125 \begin{pyconsole}
126 var = 1 + 1
127 var
128 \end{pyconsole}
129 Console variable values may be accessed inline via the \pygment{latex}{\pycon} command. More console information is available in the main documentation.
130
131 \section*{Working with Python 2}
132 \pytex\ supports both Python 2 and 3. Under Python 2, imports from \pygment{python}{__future__} will work so long as they are the first user-entered code in a given session. \pytex\ imports most things from \pygment{python}{__future__} by default. To control what is automatically imported, see the \texttt{pyfuture} and \texttt{pyconfuture} package options in the main documentation.
133
134
135 \section*{Support for additional languages}
136
137 \pytex\ also provides support for additional languages. Currently, Ruby, Julia, Octave, Sage, Bash, and Rust support is included. To enable commands and environments for these language, see the \texttt{usefamily} package option in the main documentation.
138
139 Language support is provided via a template system; in most cases, a new language can be added with about 100 lines of template code---and basic support can require less than 20 lines. If you would like support for a new language, please open an issue at \href{https://github.com/gpoore/pythontex}{GitHub}. The main documentation also contains a summary of the process for adding languages.
140
141 \section*{Macro programming}
142
143 \pytex\ commands can be used inside other commands in macro programming. They will usually work fine, but curly braces should be used as delimiters and special \LaTeX\ characters such as \texttt{\%} and \texttt{\#} should be avoided in the Python code. These limitations can be removed by passing arguments verbatim or through catcode trickery. \pytex\ environments cannot normally be used inside \LaTeX\ commands, due to the way \LaTeX\ deals with verbatim content and catcodes.
144
145
146 \section*{Additional features}
147
148 \pytex\ provides many additional features. The working and output directories can be specified via \pygment{latex}{\setpythontexworkingdir} and \pygment{latex}{\setpythontexoutputdir}. The user can determine when code is executed with the package option \texttt{rerun}, selecting factors such as modification and exit status. By default, all commands and environments with the same base name (\texttt{py}, \texttt{sympy}, \texttt{pylab}, etc.) run in a single session, providing continuity. Commands and environments accept an optional argument that specifies the session in which the code is executed; sessions run in parallel. \pytex\ provides a utilities class that is always imported into each session. The utilities class provides methods for tracking dependencies and automatically cleaning up created files. The utilities class also allows information such as page width to be passed from the \TeX\ side to Python/other languages. See the main documentation for additional information.
149
150 \pytex\ also provides the \texttt{depythontex} utility, which creates a copy of a document in which all \pytex\ commands and environments have been replaced by their output. The resulting document is more suitable for journal submission, sharing, and conversion to other document formats.
151
152 Code may be run in interactive mode on the command line via the \verb|--interactive| and \verb|--debug| options. This is primarily useful for working with interactive debuggers.
153
154
155 \section*{Customizing typesetting}
156
157 \pytex\ typesets code using the \texttt{fancyvrb} package and the \texttt{fvextra} package that extends \texttt{fancyvrb}. There is a \pygment{latex}{\setpythontexfv} command for setting \pytex-specific \texttt{fancyvrb} and \texttt{fvextra} options. The normal \pygment{latex}{\fvset} works as well for document-wide settings. \pytex\ environments take a second optional argument that consists of \texttt{fancyvrb} and \texttt{fvextra} settings. This can be used to customize automatic line breaking or line highlighting for a single environment.
158
159
160
161 \section*{Unicode support}
162
163 \pytex\ supports Unicode under all \LaTeX\ engines. For example, consider the following example from Python:
164 \begin{pygments}{python}
165 my_string = '¥ § ß Ğ Ð Ñ Ö þ ø'
166 \end{pygments}
167 This requires some engine-specific packages. Typical packages are listed below.
168
169 \begin{itemize}
170 \item pdfLaTeX:
171 \begin{pygments}{latex}
172 \usepackage[T1]{fontenc}
173 \usepackage[utf8]{inputenc}
174 \end{pygments}
175 \item LuaLaTeX:
176 \begin{pygments}{latex}
177 \usepackage{fontspec}
178 \end{pygments}
179 \item XeLaTeX:
180 \begin{pygments}{latex}
181 \usepackage{fontspec}
182 \defaultfontfeatures{Ligatures=TeX}
183 \end{pygments}
184 \end{itemize}
185
186 If you are using Python 2, you will also need to specify that you are using Unicode. You may want
187 \begin{pygments}{python}
188 from __future__ import unicode_literals
189 \end{pygments}
190 at the beginning of your Python code. Or you can just load the \pytex\ package with the option \verb|pyfuture=all|, which will import \verb|unicode_literals| automatically.
191
192
193 \end{document}