MWE

MWE

Esta é uma pergunta de acompanhamento paraControlando o recuo e o espaço vertical no topo de um tcolorbox.

@egreg forneceu uma boa solução para o problema original, mas há um efeito colateral quando certos ambientes (provavelmente qualquer coisa baseada em a trivlist) aparecem no arquivo tcolorbox. Falta o espaçamento acima do primeiro ambiente.

Como posso ajustar a resposta do @egreg para que esses ambientes fiquem espaçados corretamente?

MWE

\documentclass{article}

\usepackage{tcolorbox}

\tcbset{mybox/.style={colback=white, colframe=blue, left=2mm, right=2mm,
  fonttitle=\bfseries}, fontupper=\small,
  before upper=\setlength{\parindent}{1em}\everypar{{\setbox0\lastbox}\everypar{}},
}

\newtcolorbox{mybox}[1][]{mybox,#1}

\begin{document}
\begin{mybox}
  This line should not be indented.

  This line should be indented.

  \begin{center}
    This line should be centred. The spacing above is wrong.
  \end{center}

  This line should be indented.

  \begin{center}
    This line should be centred. The spacing above is right.
  \end{center}

  This line should be indented.
\end{mybox}

\begin{mybox}
  \section{Should not have gap above this heading}
\end{mybox}
\end{document}

Saída MWE

Responder1

O espaço vertical que falta no centerambiente interno é porque \@itemusa \addvspace\@topseppara inserir o espaço que você deseja ter ao deixar uma linha em branco antes de \begin{center}, e \addvspacenão adiciona esse espaço quando está no modo vertical e \if@minipageé verdadeiro - que é o caso dentro do seu mybox. Então, para ter esse espaço inserido, você pode definir \@minipagefalsena before upperopção do seu tcolorbox.

A lacuna vertical entre as duas caixas deve-se essencialmente ao padrão for /tcb/noparskip(ver documentação /tcb/autoparskipno tcolorboxmanual). Se você definir before=\par\noindent, você remove o adicionado \smallskip:

\documentclass{article}
\usepackage{tcolorbox}

\makeatletter

\tcbset{mybox/.style={colback=white, colframe=blue, left=2mm, right=2mm,
  fonttitle=\bfseries}, fontupper=\small,
  before upper={\setlength{\parindent}{1em}%
                \everypar{{\setbox0\lastbox}\@minipagefalse\everypar{}}},
  before=\par\noindent, after=\par
}

\makeatother

\newtcolorbox{mybox}[1][]{mybox,#1}

\begin{document}
\begin{mybox}
  This line should not be indented.

  This line should be indented.

  \begin{center}
    This line should be centered. The spacing above is correct.
  \end{center}

  This line should be indented.

  \begin{center}
    This line should be centered. The spacing above is right.
  \end{center}

  This line should be indented.
\end{mybox}

\begin{mybox}
  \section{Should not have gap above this heading}
\end{mybox}
\end{document}

Captura de tela

Se você realmente quer se livrartoda a lacuna, suprima toda a colagem vertical entre as duas caixas de interesse aqui que estão anexadas à lista vertical principal. Você pode fazer isso usando \nointerlineskip, que é usado corretamente no modo vertical por causa do que after=\pardefinimos:

\documentclass{article}
\usepackage{tcolorbox}

\makeatletter

\tcbset{mybox/.style={colback=white, colframe=blue, left=2mm, right=2mm,
  fonttitle=\bfseries}, fontupper=\small,
  before upper={\setlength{\parindent}{1em}%
                \everypar{{\setbox0\lastbox}\@minipagefalse\everypar{}}},
  before=\par\noindent, after=\par
}

\makeatother

\newtcolorbox{mybox}[1][]{mybox,#1}

\begin{document}
\begin{mybox}
  This line should not be indented.

  This line should be indented.

  \begin{center}
    This line should be centered. The spacing above is correct.
  \end{center}

  This line should be indented.

  \begin{center}
    This line should be centered. The spacing above is right.
  \end{center}

  This line should be indented.
\end{mybox}
\nointerlineskip
\begin{mybox}
  \section{Should not have gap above this heading}
\end{mybox}
\end{document}

insira a descrição da imagem aqui

Obs: Defino o \@minipagefalseinterior como temporário \everyparpara não ter espaço vertical extra dentro da caixa caso ela comece com um centerambiente. Mas se você definir diretamente no início de before upper, assim:

\tcbset{mybox/.style={colback=white, colframe=blue, left=2mm, right=2mm,
  fonttitle=\bfseries}, fontupper=\small,
  before upper={\@minipagefalse\setlength{\parindent}{1em}%
                \everypar{{\setbox0\lastbox}\everypar{}}},
  before=\par\noindent, after=\par
}

e inicie sua caixa com um centerambiente:

\begin{mybox}
  \begin{center}
    This line should be centered. The spacing above is correct.
  \end{center}

  This line should be indented.

  \begin{center}
    This line should be centered. The spacing above is correct.
  \end{center}
...
\end{mybox}

você obterá espaço extra no topo da sua caixa, assim:

insira a descrição da imagem aqui

informação relacionada