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

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


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

Tags:

Box