在对latex的使用中,我遇到了一个问题就是我需要在使用graypara的情况下对其中的一部分段落进行缩进,请问有没有大佬可以解决我的困惑。谢谢
请提供一份能体现你问题的完整代码。
\documentclass
开始到\end{document}
结束就是使用之后所有段都没有了缩进
\documentclass{article}
\usepackage{tcolorbox}
\usepackage{lipsum}
\newtcolorbox{graypara}{
colback=gray!20,
colframe=white,
boxrule=0pt,
left=5pt, right=5pt,
top=3pt, bottom=3pt,
arc=2pt,
before=\par\noindent,
after=\par\noindent
}
\begin{document}%
\lipsum[2]
\begin{graypara}%
\noindent hour = int(input('Please enter the current hour from 0 to 23: '))
\noindent if hour < 12:
\hangindent=4em
print('Good morning!')
print('Have a nice day.')
\noindent elif hour < 18:
print('Good afternoon!')
\noindent else:
print('Good evening!')
\noindent print('Have a good night.')
\end{graypara}% %注意这里后面的空行
\lipsum[2]
\end{document}
这将得到:
其实是有缩进的。
你在设置中写到:
before=\par\noindent,
after=\par\noindent,
前者可以让你的整个盒子不缩进,而后者直接让你的盒子后的下一段不缩进(如果你不用空行表示新的一段的话)...
另外,你不需要写那么多的\noindent
,似乎也不应该写\hangindent=4em
,另外,如果要实现「代码盒子」,你可以看一下文档里的tcblistings
环境,最好使用等宽字体。
我不知道你的最终目标是什么(你可以做补充),但我如果是我的话,我会修改成:
\documentclass{article}
\usepackage{showframe}
\usepackage{tcolorbox}
\tcbuselibrary{listings}
\usepackage{lipsum}
\newtcblisting{graypara}{
listing only,
colback=gray!20,
colframe=white,
boxrule=0pt,arc=2pt,
left=5pt, right=5pt,
top=3pt, bottom=3pt,
before=\par\noindent,
after=\par,
}
\begin{document}%
\lipsum[2]
% 注意tcblistings中的代码会与你源代码中的缩进保持一致
\begin{graypara}
hour = int(input('Please enter the current hour from 0 to 23: '))
if hour < 12:
print('Good morning!')
print('Have a nice day.')
elif hour < 18:
print('Good afternoon!')
else:
print('Good evening!')
print('Have a good night.')
\end{graypara}
\lipsum[2]
\lipsum[2]
\end{document}
不,不是的。不管加不加noindent和hangindent=4em都没有缩进。以下是同文章内其他效果相同的结果的代码和图片:
documentclass[utf8,oneside]{book}%LaTex的book类型中,目录及chapter前自动插入空白页面,解决方案:加入oneside选项
usepackage{titletoc}
usepackage{titlesec}
usepackage[dvipsnames]{xcolor}
usepackage{ctexcap}
usepackage[b5paper,text={125mm,195mm},centering,left=1in,right=1in,top=1in,bottom=1in]{geometry}
usepackage[]{geometry}
usepackage{imakeidx}
usepackage{multicol}
usepackage{graphicx}
usepackage{tcolorbox}
usepackage{lipsum}
usepackage[colorlinks,linkcolor=black, anchorcolor=blue,citecolor=green]{hyperref}
usepackage{ragged2e}
usepackage{caption}
usepackage{array}
usepackage{longtable}
captionsetup[table]{justification=raggedright, singlelinecheck=off}
makeindex
bibliographystyle{plain}
begin{document}
\newtcolorbox{graypara}{
colback=gray!20,
colframe=white,
boxrule=0pt,
left=5pt, right=5pt,
top=3pt, bottom=3pt,
arc=2pt,
before=\par\noindent,
after=\par\noindent
}
\begin{graypara}
\textbf{def best\_word(word\_list):}
\textbf{"""}
\textbf{ word\_list is a list of words.}
\textbf{Return the word worth the most points.}
\textbf{"""}
best\_word = ""
best\_points = 0
for word in word\_list: \#1
points = num\_points(word)
if points $>$ best\_points:
best\_word = word
best\_points = points
return best\_word
\end{graypara}
end{document}
另,不知道为什么在使用您推荐的方式去尝试完成我的代码时编译出现了错误,错误报告为:chapter_5.listing: 错误: 3: Improper alphabetic constant. ...sking the user for a password until it’s。可以请问一下为什么吗?谢谢
begin{graypara}
就是使用这个之后所有段都没有了缩进
这是代码编译后的效果图
@u155676 别人还是无法知道
graypara
环境是怎么定义的,请检查你的「文档类」,提供别人可以在电脑上复现问题的完整代码。如果是来自某个模板,请贴链接让别人知道这个环境是如何定义的。可能有关:
documentclass{article}
usepackage{tcolorbox}
newtcolorbox{graypara}{
colback=gray!20,
colframe=white,
boxrule=0pt,
left=5pt, right=5pt,
top=3pt, bottom=3pt,
arc=2pt,
before=parnoindent,
after=parnoindent
}
begin{document}
begin{graypara}{}
noindent hour = int(input('Please enter the current hour from 0 to 23: '))
noindent if hour $<$ 12:
hangindent=4em
print('Good morning!')
print('Have a nice day.')
noindent elif hour $<$ 18:
print('Good afternoon!')
noindent else:
print('Good evening!')
noindent print('Have a good night.') #1
end{graypara}
end{document}
@u155676 maybe related https://ask.latexstudio.net/ask/question/17329.html
@u155676 同时建议阅读一下这里的「使用markdown整理代码块的内容」,让你的代码正确高亮,且加上
\
. 建议「编辑」你上面评论中的代码。不,不是的。不管加不加noindent和hangindent=4em都没有缩进。以下是同文章内其他效果相同的结果的代码和图片:
documentclass[utf8,oneside]{book}%LaTex的book类型中,目录及chapter前自动插入空白页面,解决方案:加入oneside选项
usepackage{titletoc}
usepackage{titlesec}
usepackage[dvipsnames]{xcolor}
usepackage{ctexcap}
usepackage[b5paper,text={125mm,195mm},centering,left=1in,right=1in,top=1in,bottom=1in]{geometry}
usepackage[]{geometry}
usepackage{imakeidx}
usepackage{multicol}
usepackage{graphicx}
usepackage{tcolorbox}
usepackage{lipsum}
usepackage[colorlinks,linkcolor=black, anchorcolor=blue,citecolor=green]{hyperref}
usepackage{ragged2e}
usepackage{caption}
usepackage{array}
usepackage{longtable}
captionsetup[table]{justification=raggedright, singlelinecheck=off}
makeindex
bibliographystyle{plain}
begin{document}
newtcolorbox{graypara}{
}
end{document}

另,不知道为什么在使用您推荐的方式去尝试完成我的代码时编译出现了错误,错误报告为:chapter_5.listing: 错误: 3: Improper alphabetic constant. ...sking the user for a password until it’s。可以请问一下为什么吗?谢谢