在下面的代码中,我对右括号的格式有一个问题。我不知道为什么。
我给出了这个长时间测试且丑陋的代码,因为对于较小的代码我没有遇到麻烦。
\documentclass[french]{article}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\setlength{\parindent}{0cm}
% == PACKAGES USED == %
\usepackage{xargs}
\usepackage{showexpl}
\usepackage{graphicx}
\usepackage{mathabx}
\usepackage[dvipsnames,svgnames,x11names]{xcolor}
\usepackage{mdframed}
\usepackage{newverbs}
\usepackage{listings}
% == EXTRA == %
% C-Verbatim abstract macros
\makeatletter
\newcommandx\@verbItInColor[3][%
1=codeColorNormal,%
2=codeColorBack,%
3=codeColorBorder
]{%
\ifx\relax#1\relax
\edef\txt@color{colorInlineCodeNormal}%
\else
\edef\txt@color{#1}%
\fi
\ifx\relax#2\relax
\edef\bck@color{colorInlineCodeBack}%
\else
\edef\bck@color{#2}%
\fi
\ifx\relax#3\relax
\edef\border@color{colorInlineCodeBorder}%
\else
\edef\border@color{#3}%
\fi
\Collectverb{\@@myverb{\txt@color}{\bck@color}{\border@color}}%
}
\def\@@myverb#1#2#3#4{%
\lstset{basicstyle=\ttfamily\color{#1}}%
\fboxrule=\inlineCodeBorderWidth%
\ifnum\inlineCodeBorderWidth=0
\fboxsep=0pt%
\else
\fboxsep=\inlineCodeInnerMargin%
\fi
\fcolorbox{#3}{#2}{\vphantom{Mg}\lstinline|#4|}%
}
\makeatother
% == DEF : all the specifications for languages == %
% So as to have one bold face effect !
%
% Source :
% * http://tex.stackexchange.com/questions/27663/using-bold-italic-text-inside-listings
\renewcommand{\ttdefault}{pcr}
% Sources :
% * https://groups.google.com/group/fr.comp.text.tex/browse_thread/thread/903edf3522db814f?hl=fr
% * http://forum.mathematex.net/latex-f6/mettre-un-titre-a-ses-boites-t12446-20.html#p120797
% * http://tex.stackexchange.com/questions/5623/operator-highlighting-using-listings
% * ????
\makeatletter
% Default settings for listings
\definecolor{inlineCodeColorNormal}{named}{black}
\definecolor{inlineCodeColorBack}{gray}{0.95}
\definecolor{inlineCodeColorBorder}{named}{black}
\newlength{\inlineCodeBorderWidth}
\setlength{\inlineCodeBorderWidth}{0pt}
\newlength{\inlineCodeInnerMargin}
\setlength{\inlineCodeInnerMargin}{2pt}
\definecolor{codeColorNormal}{named}{black}
\definecolor{codeColorBack}{gray}{0.95}
\definecolor{codeColorBorder}{named}{black}
\newlength{\codeBorderWidth}
\setlength{\codeBorderWidth}{0pt}
\newlength{\codeInnerMargin}
\setlength{\codeInnerMargin}{2pt}
% Restore the default settings for listings
\newcommand{\codeDefaultSettings}{
\definecolor{inlineCodeColorNormal}{named}{black}
\definecolor{inlineCodeColorBack}{gray}{0.95}
\definecolor{inlineCodeColorBorder}{named}{black}
\setlength{\inlineCodeBorderWidth}{0pt}
\setlength{\inlineCodeInnerMargin}{2pt}
\definecolor{codeColorNormal}{named}{black}
\definecolor{codeColorBack}{gray}{0.95}
\definecolor{codeColorBorder}{named}{black}
\setlength{\codeBorderWidth}{0pt}
\setlength{\codeInnerMargin}{2pt}
}
% Change colors for codes
\newcommandx\changeCodeColor[3][%
1=codeColorNormal,%
2=codeColorBack,%
3=codeColorBorder
]{%
\ifx\relax#1\relax
\edef\txt@color{codeColorNormal}%
\else
\edef\txt@color{#1}%
\fi
\ifx\relax#2\relax
\edef\bck@color{codeColorBack}%
\else
\edef\bck@color{#2}%
\fi
\ifx\relax#3\relax
\edef\border@color{codeColorBorder}%
\else
\edef\border@color{#3}%
\fi
\colorlet{codeColorNormal}{\txt@color}
\colorlet{codeColorBack}{\bck@color}
\colorlet{codeColorBorder}{\border@color}
}
% Frame for blocks of listings
\mdfdefinestyle{codeFrameStyle}{
backgroundcolor = codeColorBack,%
linecolor = codeColorBorder,%
linewidth = \codeBorderWidth,%
innerleftmargin = \codeInnerMargin,%
innerrightmargin = \codeInnerMargin,%
innertopmargin = \codeInnerMargin,%
innerbottommargin = \codeInnerMargin,%
}
\newcommand{\backreturn}{\mbox{\rotatebox[y=0.9ex]{180}{\color{red} $\Lsh$}}}
\lstdefinestyle{default}{%
% Colorss
basicstyle = \ttfamily\color{codeColorNormal},
backgroundcolor = \color{codeColorBack},%
% Number for lines
numbers = left, % Where to put the line-numbers
numberstyle = \footnotesize, % Size of the fonts used for the line-numbers
stepnumber = 1, % Step between two numbers
numbersep = 7pt, % How far the line-numbers are from the code
% Back returns for long lines
breaklines = false, % Bug with inline mode !
breakatwhitespace = false, % Automatic breaks only happen at whitespace ?
breakindent = 0pt, % Space before the text of a break
postbreak = % Character used at the begining of a break
{\,\,\backreturn\,}, %
% You can use a character at the end of line where there is one break.
% * prebreak=\mbox{\tiny$\searrow$},
%
% Spacings and tabs
showstringspaces = false, % Underline spaces within strings
tabsize = 4, % Default tabsize to 4 spaces
% tab = {%
% \leavevmode\vrule width0.2pt height 1ex depth 0.8pt
% \vrule width 1.7em height0.4pt depth 0.8pt
% \vrule width0.2pt height 1ex depth 0.8pt%
% }, %
% Escaping character used to allow LaTeX formatting inside one listing.
escapechar = \⠶,%
% Playing with colors
% * Comment
commentstyle = \itshape\color{orange},
% * String
stringstyle = \itshape\color{olive},
% * Test, loop
keywordstyle = [1]\bfseries\color{red},
% * Function
keywordstyle = [2]\bfseries\color{blue},
% * Type
keywordstyle = [3]\bfseries\color{green},
% * Constant
keywordstyle = [4]\CodeConstant,
% * Local functions
keywordstyle = [5]\bfseries\color{ProcessBlue},
% * Local constants
keywordstyle = [6]\bfseries\color{gray},
}
\newcommand{\CodeConstant}[1]{\textbf{\textcolor{Maroon}{#1}}}
\newcommand{\CodeSymbol}[1]{\textbf{#1}}
\newcommand{\CodePonctuation}[1]{\textbf{#1}}
% Redefinition of some languages
%
% Here we use more colors but not too much.
\lstdefinelanguage{python}{%
% Source-TeXgraph : http://texgraph.tuxfamily.org/forum/viewtopic.php?f=4&t=275&p=2659&hilit=Voil%C3%A0+c%27est+fait#p2663
% Source-Python : http://www.developpez.net/forums/d1207390/autres-langages/python-zope/general-python/grouper-mots-cles-langage-python-categories/
%
% --- SPECIAL WORDS - Start --- %
%
% * Test, loop
classoffset=0,
morekeywords={
if,elif,else,
is,not,and,or,
for,in,while,
assert,try,except,finally
},
% * Function, class
classoffset=1,
morekeywords={
from,import,as,
global,nonlocal,
class,def,
del,
exec,lambda,
print,
return,yield,
with,access,raise,
continue,pass,break,
},
% * Type
% * Constant
classoffset=2,
morekeywords={
True,False,
None
},
% * Local functions
classoffset=3,
alsoletter={@_},
morekeywords={
@decorator,
abs,dict,help,min,setattr,
all,dir,hex,next,slice,
any,divmod,id,object,sorted,
ascii,enumerate,input,oct,staticmethod,
bin,eval,int,open,str,
bool,exec,isinstance,ord,sum,
bytearray,filter,issubclass,pow,super,
bytes,float,iter,print,tuple,
callable,format,len,property,type,
chr,frozenset,list,range,vars,
classmethod,getattr,locals,repr,zip,
compile,globals,map,reversed,__import__,
complex,hasattr,max,round,
delattr,hash,memoryview,set,
},
% * Types
classoffset=4,
morekeywords={
typeInt
},
% * Local constants
classoffset=4,
morekeywords={
NotImplemented,Ellipsis,__debug__
},
% --- SPECIAL WORDS - END --- %
classoffset=0,
% Symbols
literate=
{>}{{\CodeSymbol{>}}}1
{<}{{\CodeSymbol{<}}}1
{+}{{\CodeSymbol{+}}}1
{*}{{\CodeSymbol{*}}}1
{==}{{\CodeSymbol{= =}}}1
{=}{{\CodeSymbol{=}}}1
% Ponctuations
{]}{{\CodePonctuation{]}}}1
{[}{{\CodePonctuation{[}}}1
{(}{{\CodePonctuation{(}}}1
{)}{{\CodePonctuation{)}}}1
{:}{{\CodePonctuation{:}}}1
{,}{{\CodePonctuation{,}}}1,
% Sensitive to case
sensitive=true,%
% Comments
morecomment=[l]\#,%
% String
morestring=[b]',%
morestring=[b]",%
morecomment=[s]{'''}{'''},% used for documentation text
morecomment=[s]{"""}{"""}%
}%
\lstdefinelanguage{pythonPlus}[]{python}{%
% * Constant
classoffset=2,
morekeywords={
Plus
},
classoffset=0
}%
\newcommand\python{%
\@ifstar{%
\lstset{language=python, style=default, showspaces=true, showtabs=true}%
\@verbItInColor%
}{%
\lstset{language=python, style=default, showspaces=false, showtabs=false}%
\@verbItInColor%
}%
}
\lstnewenvironment{codePython}{%
\lstset{%
language = python,%
style = default,%
breaklines = true,%
showspaces = false,%
showtabs = false,%
}%
}{%
\lstset{breaklines=false}%
}
\BeforeBeginEnvironment{codePython}{%
\vspace{0.5em}%
\lstset{%
basicstyle = \ttfamily\color{codeColorNormal},%
backgroundcolor = \color{codeColorBack}%
}%
\begin{mdframed}[style=codeFrameStyle]%
\vspace{-0.7em}%
}
\AfterEndEnvironment{codePython}{%
\vspace{-0.5em}%
\end{mdframed}%
\vspace{0.5em}%
}
\lstnewenvironment{codePython*}{%
\lstset{%
language = python,
style = default,
breaklines = true,%
showspaces = true,%
showtabs = true,%
}%
}{%
\lstset{breaklines=false}%
}
\BeforeBeginEnvironment{codePython*}{%
\vspace{0.5em}%
\lstset{%
basicstyle = \ttfamily\color{codeColorNormal},%
backgroundcolor = \color{codeColorBack}%
}%
\begin{mdframed}[style=codeFrameStyle]%
\vspace{-0.7em}%
}
\AfterEndEnvironment{codePython*}{%
\vspace{-0.5em}%
\end{mdframed}%
\vspace{0.5em}%
}
\lstnewenvironment{codePythonPlus}{%
\lstset{%
language = pythonPlus,
style = default,
breaklines = true,%
showspaces = false,%
showtabs = false,%
}%
}{%
\lstset{breaklines=false}%
}
\BeforeBeginEnvironment{codePythonPlus}{%
\vspace{0.5em}%
\lstset{%
basicstyle = \ttfamily\color{codeColorNormal},%
backgroundcolor = \color{codeColorBack}%
}%
\begin{mdframed}[style=codeFrameStyle]%
\vspace{-0.7em}%
}
\AfterEndEnvironment{codePythonPlus}{%
\vspace{-0.5em}%
\end{mdframed}%
\vspace{0.5em}%
}
\makeatother
\begin{document}
\section{Python}
\python+for i in range(9):# Une Boucle+ est du code dans une ligne,
et voici ci-dessous du code sur plusieurs lignes.
\begin{codePython}
@decorator
while(True):
# Une boucle infinie
print("Du texte, toujours et encore ==")
typeInt a = 6
typeInt a += 7
b = [a, a**2]
if a == b:
print('N\'importe quoi !')
else:
return None
\end{codePython}
Le même code avec tous les espaces visibles.
\begin{codePython*}
while(True):
# Une boucle infinie
print("Du texte, toujours et encore")
\end{codePython*}
\begin{codePythonPlus}
while(True): Plus
# Une boucle infinie
print("Du texte, toujours et encore")
\end{codePythonPlus}
\end{document}
答案1
您的代码确实很重。问题出在选项上breaklines=true
。这就是整个魔法 ;-)。如果您使用此选项listings
,则会更改 catcode )
。因此选项literate
失败。