8

In LaTeX, the way \begin{center}....\end{center} spaces the tcolorbox is different from how \centering does it. The center environment, provides enough space but the vertical spacing done with \centering isn't the same as with center environment. Now my question is, when using \centering, how do I achieve the same spacing of tcolorbox as done with center environment.

Here is the MWE:

\documentclass[12pt]{article}
\usepackage[many]{tcolorbox}
\usepackage{kantlipsum}

\newcommand{\unittcbox}[1]{%
\medskip \centering \tcbox{#1} \medskip
}%
\begin{document}
\kant[1]
\unittcbox{SI unit :  Ampere. square metre ($A.m²$)}
\kant[2]
\begin{center}
\tcbox{SI unit :  Ampere. square metre ($A.m²$)}
\end{center}
\kant[3]
\end{document}

I tried with \medskip but it didn't workout.

Using centering:

enter image description here

Using center environment:

enter image description here

subham soni
  • 9,673
  • 1
    this is not really about tcolorbox, you should check this: http://tex.stackexchange.com/a/23653/24483 what you're looking for is the difference between center environment and \centering – d-cmst May 18 '14 at 16:50
  • No @dcmst, it didnt help me solve the problem. It says add a \par, tried it but no use :( – subham soni May 18 '14 at 16:54
  • 1
    I mean the part where it says that center adds a \trivlist. See the definition of \center: \def\center{\trivlist \centering\item\relax} and \def\endcenter{\endtrivlist} – d-cmst May 18 '14 at 16:58
  • \centering doesn't do any vertical spacing it just sets the paragraph alignment to center, the center environment does \centering but also adds vertical space. Your question isn't at all clear, but if you want the spacing of the center environment why not use that? – David Carlisle May 18 '14 at 20:28

2 Answers2

8

May be this is what you want.

\documentclass[12pt]{article}
\usepackage[many]{tcolorbox}
\usepackage{siunitx}
\usepackage{kantlipsum}
\newtcolorbox{units}{before=\par\smallskip\centering,after=\par,hbox}

\begin{document}
\kant*[2]
\begin{units}
SI unit :  Ampere. square metre (\si{A.m^2})
\end{units}
Some text comes here just for demo.
\kant[3]
\end{document}

enter image description here

I have defined a new environment so that you don't have to type much every time.

If you want more space, you may use

\newtcolorbox{units}{before=\begin{center},after=\end{center},hbox}

or manually adjust the space as in

\newtcolorbox{units}{before=\par\bigskip\centering,after=\par\bigskip,hbox}
5

My impression is that you want to use the on line option:

\documentclass[12pt]{article}
\usepackage[many]{tcolorbox}
\usepackage{siunitx}
\usepackage{kantlipsum}

\begin{document}
\kant*[2]
\begin{center}
\tcbox[on line]{SI unit :  Ampere. square metre (\si{A.m^2})}
\end{center}
\kant[3]
\end{document}

enter image description here

egreg
  • 1,121,712