如何仅在 tcolorbox 中缩进第一行

如何仅在 tcolorbox 中缩进第一行

如何仅缩进第一行tcolorbox

\documentclass[12pt,a4paper]{book}
\usepackage[listings,many]{tcolorbox}
\usetikzlibrary{calc}
\usepackage{lipsum}

\tcbuselibrary{listingsutf8,breakable,skins}
\tcbset{listing engine=listings}
\tcbset{mystyle/.style={
  breakable,
  enhanced,
  rightrule=0pt,
  toprule=0pt,
  outer arc=0pt,
  before upper={\parindent17pt},
parbox = true,
  arc=0pt,
  colback=white,
  attach boxed title to top left,
  fonttitle=\sffamily
  }
}
\newtcolorbox[auto counter]{mybox}[1][]{
  mystyle,colframe=black,boxed title style={
    colback=black,
    outer arc=0pt,
    arc=0pt,
    top=0pt,
    bottom=0pt,
left=1pt,
right=1pt
    },
left=0pt,right=0pt,top=-18pt,bottom=0pt, 
  title=\thetcbcounter,
  overlay unbroken and first={
      \path
        let
        \p1=(title.north west),
        \p2=(frame.north west)
        in
        node[anchor=west,font=\sffamily,text width=\x2-\x1]
        at (title.west) {#1};
  }
}
\begin{document}


\begin{mybox}
\lipsum[1-8]
\end{mybox}

\end{document}

在此处输入图片描述

答案1

您已将 设置\parindent17pt,但您实际上只想为编号标题框腾出空间。因此您实际上需要\parindent=0pt,然后在第一段中进行手动缩进。

您可以使用以下方法实现此目的:

before upper={\setlength{\parindent}{0pt}\hspace*{17pt}}

这是一个完整的例子:

\documentclass[12pt,a4paper]{book}
\usepackage[listings,many]{tcolorbox}
\usetikzlibrary{calc}
\usepackage{lipsum}

\tcbuselibrary{listingsutf8,breakable,skins}
\tcbset{listing engine=listings}
\tcbset{mystyle/.style={
  breakable,
  enhanced,
  rightrule=0pt,
  toprule=0pt,
  outer arc=0pt,
  before upper={\setlength{\parindent}{0pt}\hspace*{17pt}},
parbox = true,
  arc=0pt,
  colback=white,
  attach boxed title to top left,
  fonttitle=\sffamily
  }
}
\newtcolorbox[auto counter]{mybox}[1][]{
  mystyle,colframe=black,boxed title style={
    colback=black,
    outer arc=0pt,
    arc=0pt,
    top=0pt,
    bottom=0pt,
left=1pt,
right=1pt
    },
left=0pt,right=0pt,top=-18pt,bottom=0pt, 
  title=\thetcbcounter,
  overlay unbroken and first={
      \path
        let
        \p1=(title.north west),
        \p2=(frame.north west)
        in
        node[anchor=west,font=\sffamily,text width=\x2-\x1]
        at (title.west) {#1};
  }
}
\begin{document}


\begin{mybox}
\lipsum[1-2]
\end{mybox}

\end{document}

代码输出

相关内容