# How to put a box around text and add a string?

I have some \items that I would like to put in a box. I would like the box to have 2 arguments that I can pass

• color of the box

• string for the box

Relevant Research:

I couldn't find anything relevant, but the nearest I could find was \parbox

MWE:

\documentclass[hidelinks,a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\geometry{
a4paper,
total={170mm,257mm},
left=20mm,
top=20mm,
}
\usepackage{mathptmx}  % Times New Roman
\usepackage{setspace}  % Double spacing

\newcommand*\ruleline[1]{\par\noindent\raisebox{.8ex}{\makebox[\linewidth]{\hrulefill\hspace{1ex}\raisebox{-.8ex}{\textsc{#1}}\hspace{1ex}\hrulefill}}}

\setlength{\parindent}{0pt}
\doublespacing

\begin{document}

\ruleline{Questions}

\begin{enumerate}
\item How would I be able to make a box to surround  \verb!\item! with \LaTeX{}?
\item It should have an argument for string...
\item ...and another argument for a colour choice

\ruleline{More Questions}
\item The box should look something like this
\item minimum two to three \verb!\item!s high
\item some stuff
\item some more stuff
\item some more stuff

\end{enumerate}

\end{document}


Ideal Output:

Question:

How can I change my MWE code to get the ideal output?

• Jan 11, 2020 at 9:49
• Jan 11, 2020 at 9:54
• @DavidPurton Thanks! But the samples there appear to be to textbook-esque and fancy. I just want something simple - thin lines with 90* corners with two arguments \begin{mybox}{Part A}{red} as opposed to textbook-esque boxes with the highlighted Jan 11, 2020 at 10:22
• tcolorbox is extremely flexible. You can make the boxes look as simple or as fancy as you want. Jan 11, 2020 at 10:24

Based on David Purton previous answer, a complete solution here (with the enumitem package, which provide a resume mecanism between several enumerate environments).

I add a leftmargin of 1.54cm (1 inch minus 1 cm) in the enumerate environment outside mybox environment, for aligning item numbers.

As you can see, for boxed items, the enumerate environment must be inside the mybox environment.

\documentclass[hidelinks,a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\geometry{
%a4paper, <-- commented because this is a global option in the documentclass
total={170mm,257mm},
left=20mm,
top=20mm,
}

\usepackage[skins]{tcolorbox}
\usepackage{varwidth}

\makeatletter
\newtcolorbox{mybox}[1][]{
enhanced,
colframe=black, colback=white,
sharp corners,
boxrule=0.6pt,
detach title,
coltitle=black,
colbacktitle=white,
fonttitle=\footnotesize,
overlay={
\node[rotate=90,
fill=white,
font=\kvtcb@fonttitle,
minimum width=1cm]
at (frame.west)
{\begin{varwidth}{\tcbtextheight}
\centering\tcbtitle\par
\end{varwidth}};
},#1}
\makeatother

\newcommand*\ruleline[1]{\par\noindent\raisebox{.8ex}{\makebox[\linewidth]{\hrulefill\hspace{1ex}\raisebox{-.8ex}{\textsc{#1}}\hspace{1ex}\hrulefill}}}

\usepackage{enumitem} % for resuming a list

\begin{document}
\ruleline{Hello}

\begin{mybox}[title=Part A]
\begin{enumerate}[series=g_cntr] % g_cntr: a global counter
\item How would I be able to make a box to surround  \verb!\item! with \LaTeX{}?
\item It should have an argument for string...
\item ...and another argument for a colour choice and another very very very very pointlessly long string to test the box.
\end{enumerate}
\end{mybox}

\begin{enumerate}[leftmargin=1.54cm,resume=g_cntr] % 1.54cm = 1in - 1cm
\item ...and another argument for a colour choice
\end{enumerate}

\ruleline{The second Hello}

\begin{mybox}[colframe=red, title=Part B - Very Important]
\begin{enumerate}[resume=g_cntr]
\item minimum two to three \verb!\item!s high
\item some stuff
\item some more stuff
\item some more stuff
\end{enumerate}
\end{mybox}

\begin{enumerate}[leftmargin=1.54cm,resume=g_cntr] % 1.54cm = 1in - 1cm
\item continue
\item as if there are no boxes
\end{enumerate}
\end{document}


• Thanks! If the OP is interested, it's also possible to extend the tcolorbox into the margin. Jan 12, 2020 at 10:04
• @DavidPurton & quark67 Thank you guys so much! <3 LaTeX angels <3 Jan 12, 2020 at 10:34
• @DavidPurton probably better if we could make the boxes wider so I don't need to use the [leftmargin=1.54cm,resume=g_cntr] - I'm so shy in asking this :/ but its for the love of LaTeX <3 Jan 12, 2020 at 11:01
• @3kstc, I had a go for you. It's not quite perfect, but best I can do. Jan 12, 2020 at 13:04
• You guys are LaTeX gods <3 thank you so much for all your help :D Jan 12, 2020 at 13:21

This should get you started. I've used an overlay and a tikz node to put the title on the left border. The variable width is done with a varwidth environment.

Update to address more specifically the original question layout.

I've used enumitem to resume the numbering, but I'm unable to set this up completely automatically. See enumitem, is possible to put "series=<name>" in \setlist?. So it's still necessary to use \begin{enumerate}[start=1, series=questions] whenever you want a list starting with one.

I've widened the box so it juts out into the left margin enough for the title, and the right margin enough so that the text width in the box is the same as the text width in normal text. This is done using these tcolorbox options:

enlarge left by=-8mm,
enlarge right by=-2mm,
width=\linewidth+10mm,
left=7mm,
right=2mm,


I've also added some code to before upper to make indents and spacing within boxes more consistent with normal text. See my questions here and here.

# MWE

\documentclass[a4paper,12pt]{article}

\usepackage{xhfill}
\usepackage{enumitem}
\usepackage[skins]{tcolorbox}
\usepackage{varwidth}

\makeatletter
\newtcolorbox{mybox}[1][]{
enhanced,
colframe=black, colback=white,
sharp corners,
boxrule=0.6pt,
detach title,
coltitle=black,
colbacktitle=white,
fonttitle=\footnotesize,
enlarge left by=-8mm,
enlarge right by=-2mm,
width=\linewidth+10mm,
left=7mm,
right=2mm,
before upper=\setlength{\parindent}{17.62482pt}\everypar{{\setbox0\lastbox}\@minipagefalse\everypar{}},
overlay={
\node[rotate=90,
fill=tcbcolbacktitle,
font=\kvtcb@fonttitle,
minimum width=1cm]
at (frame.west)
{\begin{varwidth}{\tcbtextheight}%
\centering\tcbtitle\par
\end{varwidth}};
},#1}
\makeatother

\setlist[enumerate,1]{resume=questions}

\vskip 2mm
\noindent\strut\xrfill{0.6pt}\strut\space\textsc{#1}\space\xrfill{0.6pt}\par
\vskip 2mm}

\begin{document}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
mauris.

\begin{mybox}[title=Part A]
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum
gravida mauris.
\begin{enumerate}[start=1, series=questions]
\item How would I be able to make a box to surround  \verb!\item! with \LaTeX{}?
\item It should have an argument for string...
\item ...and another argument for a colour choice
\end{enumerate}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum
gravida mauris.
\end{mybox}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
mauris.

\begin{enumerate}
\item The box should look something like this
\end{enumerate}

\begin{mybox}[colframe=red, title=Part B - Some very long text that goes for
two or three lines]
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum
gravida mauris.
\begin{enumerate}
\item minimum two to three \verb!\item!s high
\item some stuff
\item some more stuff
\item some more stuff
\end{enumerate}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum
gravida mauris.
\end{mybox}

\end{document}


• @3kstc I guess Overleaf's version of tcolorbox or tikz is too old. Just use fill=white instead of fill=tcbcolbacktitle. Jan 11, 2020 at 11:27
• @3kstc, well your structure is all messed up. You have everything inside an enumerate environment but no \item. Have a look at the other answer provided by quark67 to see how to do what you want. Jan 12, 2020 at 9:59