2

I have the following situation, where I have produced two boxes with text inside using the package tcolorbox:

\documentclass[a4paper,12pt]{article}
\usepackage[english]{babel}
\usepackage[most]{tcolorbox}

\begin{document}

\begin{tcolorbox}[colback=black!8!white,colframe=black, width=3cm]
\centering Males
\end{tcolorbox}
\begin{tcolorbox}[colback=black!8!white,colframe=black, width=3cm]
\centering Females
\end{tcolorbox}

\end{document}

However, with this code both boxes appear in different lines, and I would like to have them on the same line and to be able to adjust the distance between them using \hspace*{}.

How can this be done, preferably in a simple way?

Edit: the answers I received here are way more helpful, short and elegant than those provided in the related question that caused the close votes. For this reason, I don't believe the question should be closed.

3
  • 1
    I think the option you need is on line.
    – campa
    Commented May 17 at 10:43
  • @campa Thank you for the suggestion, it is helpful, but a bit too complicated. The solution that another person posted as an answer seems much better. Commented May 17 at 11:01
  • I don't see how adding on line should be more complicated than adding before=,after=.
    – campa
    Commented May 17 at 11:14

2 Answers 2

8

You can use the before and after options:

\documentclass{article}
\usepackage{tcolorbox}

\begin{document}

\begin{tcolorbox}[colback=black!8,width=3cm,halign=center,after=\hspace{1cm}]
Males
\end{tcolorbox}
\begin{tcolorbox}[colback=black!8,width=3cm,halign=center,before=]
Females
\end{tcolorbox}

\end{document}

enter image description here

1
  • You are a wizard. This is perfect and so elegant. Thank you! Commented May 17 at 11:00
7

By adopting the example from the documentation p.14.

\documentclass[a4paper,12pt]{article}
\usepackage{tcolorbox}
\usepackage{showframe}
\begin{document}
\tcbset{colback=black!8!white,colframe=black, width=3cm,nobeforeafter}
\noindent\begin{tcbox}
{Males}
\end{tcbox}%
\hspace{1cm}\begin{tcbox}
{Females}
\end{tcbox}
\end{document}

enter image description here

Not the answer you're looking for? Browse other questions tagged .