如何在graypara中使用缩进?

发布于 2025-08-10 19:35:14

在对latex的使用中,我遇到了一个问题就是我需要在使用graypara的情况下对其中的一部分段落进行缩进,请问有没有大佬可以解决我的困惑。谢谢

查看更多

关注者
0
被浏览
105
2 个回答
Sagittarius Rover
我要成为Typst糕手/(ㄒoㄒ)/~~

What is 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}

这将得到:

image.png

其实是有缩进的。


一些探索

你在设置中写到:

before=\par\noindent,
after=\par\noindent,

前者可以让你的整个盒子不缩进,而后者直接让你的盒子后的下一段不缩进(如果你不用空行表示新的一段的话)...

另外,你不需要写那么多的\noindent,似乎也不应该写\hangindent=4em,另外,如果要实现「代码盒子」,你可以看一下文档里的tcblistings环境,最好使用等宽字体。

我不知道你的最终目标是什么(你可以做补充),但我如果是我的话,我会修改成:

注意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}

image.png

我爱大雄
我爱大雄 5小时前
这家伙很懒,什么也没写!

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

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览