8

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:

enter image description here

Question:

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

4

2 Answers 2

3

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}

enter image description here

5
  • 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
    – 3kstc
    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
    – 3kstc
    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
    – 3kstc
    Jan 12, 2020 at 13:21
9

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}

\newcommand{\questionheading}[1]{%
  \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.

\questionheading{Questions}

\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.

\questionheading{More Questions}

\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}

MWE output

2
  • 1
    @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

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.