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

4
  • 1
    this is not really about tcolorbox, you should check this: tex.stackexchange.com/a/23653/24483 what you're looking for is the difference between center environment and \centering
    – Old Nick
    May 18, 2014 at 16:50
  • No @dcmst, it didnt help me solve the problem. It says add a \par, tried it but no use :( May 18, 2014 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}
    – Old Nick
    May 18, 2014 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? May 18, 2014 at 20:28

2 Answers 2

7

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}
2
  • But, the spacing is a bit less I feel. Can it be increased to similar of \center enironment May 19, 2014 at 1:09
  • @subhamsoni I have added the options.
    – user11232
    May 19, 2014 at 1:25
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

1
  • Can you please edit the code in such a that, it uses \newcommand as in my MWE May 19, 2014 at 1:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.