0% found this document useful (0 votes)
30 views

Example Use of Python - Sty: June 6, 2014

The document demonstrates using the python.sty package to display Python code and output within LaTeX. It defines a function to generate LaTeX code for n×n identity matrices for n from 1 to 5. The Python code is displayed using the \begin{python} environment, and generates the identity matrices as LaTeX-formatted equations. It also shows an example that generates a deliberate exception to demonstrate error handling.

Uploaded by

Luis Fernando
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Example Use of Python - Sty: June 6, 2014

The document demonstrates using the python.sty package to display Python code and output within LaTeX. It defines a function to generate LaTeX code for n×n identity matrices for n from 1 to 5. The Python code is displayed using the \begin{python} environment, and generates the identity matrices as LaTeX-formatted equations. It also shows an example that generates a deliberate exception to demonstrate error handling.

Uploaded by

Luis Fernando
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Example Use of python.

sty
James Brotchie [email protected]
June 6, 2014
N N element identity matrices for N {1, 2, . . . , 5}: Codigo para resolver
el problema
# use en el preambulo \usepackage{python}
\begin{python}
def identity(n):
"""
Generates and returns the LaTeX code for
a n x n identity matrix.
"""
return \n.join([
r\left[,
r\begin{array}{%s} % (n*c,),
\\\\.join(&.join(1 if i==j else 0 for j in range(n))
for i in range(n)),
r\end{array},
r\right],
])
# Displays identity matrices for
# n \in {1, 2, ..., 5}.
print \n.join([
r\begin{equation*},
r\begin{array}{%s} % (5*c,),
&.join(identity(i) for i in range(1,6)),
r\end{array}.,
r\end{equation*},
])
\end{python}
La salida que obtenemos es:
1


1 0
0 1

1 0 0
0 1 0
0 0 1

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1

.
Otro Ejemplo:
\begin{python}
# Create a deliberate exception.
print This text will be displayed.
print 1/0
print This text won\t be displayed.
\end{python}
La salida es :
This text will be displayed.
Traceback (most recent call last):
File "runpy.py", line 20, in <module>
exec(code, {print: fout.write})
File "example2.py", line 3, in <module>
print 1/0
ZeroDivisionError: integer division or modulo by zero
2

You might also like