我有这个代码:
\documentclass[12pt, a4paper]{book}
\usepackage[english,bidi=default]{babel} % Typeset Arabic
\babelprovide[import,main]{arabic} % Typeset Arabic
\babelfont[arabic]{rm}[Script=Arabic]{Amiri} % Typeset Arabic {Arabic Typesetting}
\usepackage{listings} % Typeset Code
\usepackage{xcolor}
% Define Python typeset format
\lstdefinestyle{mypython}{
language=Python, % Python language syntax
basicstyle=\ttfamily\small, % Basic font style (tt = monospaced)
keywordstyle=\bfseries\color{blue}, % Keywords in bold blue
stringstyle=\color{green}, % Strings in green
commentstyle=\color{red}, % Comments in gray
numbers=left, % Line numbers on the left
numberstyle=\tiny\color{green}, % Line numbers style
stepnumber=1, % Number every line
numbersep=4mm, % Space between numbers and code
backgroundcolor=\color{white}, % Light gray background
identifierstyle=\color{purple}, % Set color for identifiers purple (variable names)
showstringspaces=false, % Don't show spaces in strings
frame=single, % Box around the code
rulecolor=\color{green}, % Color of the box around the code
framesep=3mm, % Padding around code
breaklines=true, % Break long lines
breakatwhitespace=true, % Break only at whitespace
tabsize=5, % Set tab size to 4 spaces
aboveskip=10pt, % Space above the code
belowskip=10pt, % Space below the code
captionpos=b,
}
% Set "mypython" as the default style
\lstset{style=mypython}
\begin{document}
\chapter{مثال}
هذا مثال لكود بايثون بسيط
\begin{otherlanguage}{english}
\begin{lstlisting}
# This is a simple addition program, the program askes user to input two values, then it addes them, then it displays the result.
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
result = num1 + num2
print("The result is:", result)
\end{lstlisting}
\end{otherlanguage}
\end{document}
生成以下清单,其中的问题用红色圈出:
我已经搜索过解决方案,甚至没有找到任何解释。
非常感谢您的帮助
14
最佳答案
2
您可以将其用作tcolorbox
框架。
\documentclass[12pt, a4paper]{book}
\usepackage[english,bidi=default]{babel} % Typeset Arabic
\babelprovide[import,main]{arabic} % Typeset Arabic
\babelfont[arabic]{rm}[Script=Arabic]{Amiri} % Typeset Arabic {Arabic Typesetting}
\usepackage{listings} % Typeset Code
\usepackage[many,listings]{tcolorbox}
\usepackage{xcolor}
% Define Python typeset format
\lstdefinestyle{mypython}{
language=Python, % Python language syntax
basicstyle=\ttfamily\small, % Basic font style (tt = monospaced)
keywordstyle=\bfseries\color{blue}, % Keywords in bold blue
stringstyle=\color{green}, % Strings in green
commentstyle=\color{red}, % Comments in gray
numbers=left, % Line numbers on the left
numberstyle=\tiny\color{green}, % Line numbers style
stepnumber=1, % Number every line
numbersep=4mm, % Space between numbers and code
% backgroundcolor=\color{white}, % Light gray background
identifierstyle=\color{purple}, % Set color for identifiers purple (variable names)
showstringspaces=false, % Don't show spaces in strings
% frame=single, % Box around the code
% rulecolor=\color{green}, % Color of the box around the code
% framesep=3mm, % Padding around code
breaklines=true, % Break long lines
breakatwhitespace=true, % Break only at whitespace
tabsize=5, % Set tab size to 4 spaces
% aboveskip=10pt, % Space above the code
% belowskip=10pt, % Space below the code
captionpos=b,
}
\NewTCBListing{mylisting}{!O{}}{
colback=white,
colframe=green,
sharp corners,
boxsep=-2mm,
listing only,
listing options={style=mypython,#1},
}
\AddToHook{env/mylisting/before}{\begin{otherlanguage}{english}}
\AddToHook{env/mylisting/after}{\end{otherlanguage}}
\begin{document}
\chapter{مثال}
هذا مثال لكود بايثون بسيط
\begin{mylisting}
# This is a simple addition program, the program asks user to input two values, then it adds them, then it displays the result.
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
result = num1 + num2
print("The result is:", result)
\end{mylisting}
\end{document}
1
-
谢谢@egreg,这个解决方案非常好,它甚至给了我更多的格式化选项。
–
|
[注意。图片和标题有些误导。问题是整个区块都向左移动了。]
listings
使用 格式化代码\parshape
,这在 中有些问题luatex
。尽管babel
修改了列表环境以调整其行为(LaTeX 内核中没有其他用途,iir),但可能存在依赖此命令的包。为了调整确切的行为,luatex
提供了\shapemode
。在这种情况下,以下内容解决了问题:
\begin{otherlanguage}{english}
\shapemode=2
\begin{lstlisting}
# This is a simple addition program, the program askes user to input two values, then it addes them, then it displays the result.
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
result = num1 + num2
print("The result is:", result)
\end{lstlisting}
\end{otherlanguage}
或者,如果您仅使用english
来包装列表,则可以添加序言:
\AddBabelHook[english]{shape}{afterextras}{\shapemode=2\relax}
1
-
谢谢@ Javier Bezos,我也将尝试这个。
–
|
–
–
–
–
–
|