1

I'm not really a LaTeX programmer but have to go through the code of a document that I didn't write, and I'm wondering how to create a very basic box using the tcolorbox package. I have the manual that explains how it all works, but it's overwhelming to a LaTeX-noob like myself, so I'm just hoping someone can simplify things for me.

All I want to do is define a particular type of box (call it \qbox for example) in the .sty document and then call it when I need it. It just needs to be a 5mm square gray box with a white number (title) in it. How can I define and call a simple box like this with tcolorbox?

The boxes should look as close to these as possible:

gray boxes

  • Welcome to TeX.SX! You can do it with the standard \colorbox, I think. – egreg Jan 15 '18 at 7:31
  • Perfect! I didn't even know about \colorbox but it does the job fine and isn't too complicated to use. I just did \colorbox{gray}{\textcolor{white}{\textbf{\,1\,}}} and it pretty much looks the way I want. – James Rodkey Jan 15 '18 at 8:03
3

(Principally the frame is stolen from Torbjørn ;-), but this is one way to go for tcolorbox, defining a total TCBox:

\documentclass{article}
\usepackage{xcolor}

\usepackage[most]{tcolorbox}


\DeclareTotalTCBox[auto counter,number within=section]{\qbox}{+O{}}{
  enhanced,
  frame hidden,
  sharp corners,
  nobeforeafter,
  colback=gray,
  colupper=white,
  size=small,
  box align=base}{%
  \thetcbcounter% Declare the content immediately, there the box number
}
\begin{document}
\section{Foo}
\qbox{}
And then some text and stuff.
\qbox{}
And so on. \qbox\qbox\qbox\qbox\qbox\qbox\qbox

\section{Bar}
Next section, start again with \qbox.
\end{document}

enter image description here

  • Is the size of the box here (the width specifically) defined with the size=small parameter? – James Rodkey Jan 16 '18 at 7:43
  • Yes, there are some possible values for size. Small is the quickest way, in my point of view, in order to get a small boundary – user31729 Jan 16 '18 at 8:31
2

If all your boxes are numbered sequentially, you can set up a counter to get the number automatically. In the example below I also reset the counter at every \section, as a demonstration. If you don't want that, remove the \counterwithin line.

output of code

\documentclass{article}
\usepackage{xcolor}
\usepackage{chngcntr}

\newcounter{boxnumber}
% reset counter at every \section:
\counterwithin{boxnumber}{section}

\newcommand\qbox{%
  \refstepcounter{boxnumber}%
  \colorbox{gray}{\textcolor{white}{\textbf{\arabic{boxnumber}}}}%
}

\begin{document}
\section{Foo}
\qbox{}
And then some text and stuff.
\qbox{}
And so on. \qbox\qbox\qbox\qbox\qbox\qbox\qbox

\section{Bar}
Next section, start again with \qbox.
\end{document}
  • This is a great idea - I hadn't even considered the sequential counting as well. Thanks! – James Rodkey Jan 16 '18 at 7:42

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.