如何僅在 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

您已將 設為\parindentbe 17pt,但您實際上只想為編號標題框騰出空間。所以你確實需要\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}

程式碼的輸出

相關內容