I have some code of around 350 lines, and I need to paste it inside a LaTeX file without losing the formatting such as the extra spaces. What do I need to use to get this in LaTeX?
[Tex/LaTex] Copying code to LaTeX without losing the formatting
code
Related Solutions
As others have pointed out, this is because minted only activates mathescape
inside comments.
FWIW, the same is true for the t-vim module in ConTeXt. It is similar to the minted
package for LaTeX, but uses vim
instead of pygments
for syntax highlighting.
t-vim
provides an option to load an arbitrary vim file before the source code is parsed. So, it is possible to change the parser on the fly. For example, to identify docstrings as comments, you can use the vim file given in this thread in the vim mailing list.
\usemodule[vim]
\startvimrc[name=python-docstring]
syn match pythonBlock ":$" nextgroup=pythonDocString skipempty skipwhite
syn region pythonDocString matchgroup=Normal start=+[uU]\='+ end=+'+ skip=+\\\\\|\\'+ contains=pythonEscape,@Spell contained
syn region pythonDocString matchgroup=Normal start=+[uU]\="+ end=+"+ skip=+\\\\\|\\"+ contains=pythonEscape,@Spell contained
syn region pythonDocString matchgroup=Normal start=+[uU]\="""+ end=+"""+ contains=pythonEscape,@Spell contained
syn region pythonDocString matchgroup=Normal start=+[uU]\='''+ end=+'''+ contains=pythonEscape,@Spell contained
hi def link pythonDocString Comment
\stopvimrc
\definevimtyping[PYTHON][syntax=python, extras=python-docstring]
\starttext
\startPYTHON[escape=on]
def naive(a,x):
"""
lorem ipsum....
this code will be escaped (note no spaces)
\math{a=\{a_0,a_1,a_2,a_3,\dots,a_n\}}
"""
# this code will be escaped
p = a[0] # \math{p=\sqrt{\frac{1}{3}}}
y = x
for ai in a[1:]:
p = p + ai*y
y = y*x
return p
\stopPYTHON
\stoptext
which gives:
One difference in t-vim is that you need to use \math{...}
(or \m{...}
) to enable math mode rather than $...$
. As with minted
and listings
do not use spaces in math mode.
To do something similar in minted
, you will need to change the python parser so that it identifies docstrings as comments.
The syntax for \verb
is
\verb<char><text><char>
where <char>
should be a (non special) character not found in <text>
. Most often |
or +
are used for <char>
.
A verbatim
environment doesn't make sense in a table cell belonging to a column declared as l
, c
or r
, just like a quote
environment doesn't make sense in the argument to \mbox
.
If you have multiline verbatim to be used in a cell column, you need to use a p
column, but with some adjustments.
\documentclass{report}
\usepackage{booktabs,array}
\makeatletter
\newcolumntype{V}[1]{>{\topsep=0pt\@minipagetrue}p{#1}<{\vspace{-\baselineskip}}}
\makeatother
\newcommand{\command}[1]{\texttt{\string#1}}
\setlength{\parindent}{0pt} % just for the example
\begin{document}
\begin{tabular}{ll}
\toprule
Command & Example\
\midrule
\command{\citeplist}
& \verb|\citeplist{peel_epidemiology_2011,espinoza2012inverse}|
\\
\command{\citet}
& \verb|\citet{peel_epidemiology_2011,espinoza2012inverse}|
\\
\command{\citep}
& \verb|\citep{peel_epidemiology_2011,espinoza2012inverse}|
\\
\command{\citeyear}
& \verb|\citeyear{peel_epidemiology_2011}|
\\
\command{\citeauthor}
& \verb|\citeauthor{peel_epidemiology_2011}|
\\
\command{\citetlist}
& \verb|\citetlist{peel_epidemiology_2011,espinoza2012inverse}|
\\
\command{\citeplist}
& \verb|\citeplist{peel_epidemiology_2011,espinoza2012inverse}|
\\
\bottomrule
\end{tabular}
\bigskip
\begin{tabular}{lV{280pt}}
\toprule
Command & Example\\
\midrule
\command{\citeplist} &
\begin{verbatim}
\citeplist{peel_epidemiology_2011,espinoza2012inverse,
espinoza2012optimization
\end{verbatim}
\\
\command{\citet} &
\begin{verbatim}
\citet{peel_epidemiology_2011,espinoza2012inverse,
espinoza2012optimization
\end{verbatim}
\\
\command{\citeyear} &
\begin{verbatim}
\citeyear{peel_epidemiology_2011}
\end{verbatim}
\\
\bottomrule
\end{tabular}
\end{document}
Best Answer
You can use the package
\usepackage{listings}
and put your code in the following wayFor information, you can read the wikibook LaTeX/Source Code Listings at https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings