[Tex/LaTex] framed or colored box with text and margin notes

framedmarginnoteminipagetcolorbox

I am using pdfTeX and I have in my text an example which just is a kind of a short story. What I want is:

  1. The story should be visually distinct from the normal text, by frame or background colour.
  2. I would like to place margin notes like note 1, note 2 beside that story
    so that I can refer to them later. These notes are part of the story, so
    inside frame or with the same background colour.
  3. Ideally, I would like latex to make the label and reference from and
    to these notes but as the number of notes is rather small I guess I could do that by hand.

I would like to use the tcolorbox package perhaps with the marginnote package
but I can't get them do what I want. Also looked at minipage but no success
either.
I added an example how it almost should look like, only the step's in the margin should get inside the box.

\documentclass[12pt]{article}
\usepackage[many]{tcolorbox}
\usepackage{marginnote}
\usepackage{lipsum}
\tcbuselibrary{skins,breakable}
\newtcolorbox{story}[1][]{
  enhanced,
  breakable,
  pad at break=2mm,
  left=2mm,right=30mm,
  colback=white,colframe=black!50!yellow,
  drop fuzzy midday shadow=black!50!yellow, 
  width={0.95\textwidth}, 
  frame hidden, 
  segmentation hidden,
  before=\par\vspace*{2mm},after=\par\bigskip,
  title=#1}%



\begin{document}
\lipsum[1]
\begin{story}[Some story]
Some text comes\footnote{a footnote} here just\marginnote{1. step} for demo.
\lipsum[1]
\end{story}
\lipsum[1]
\end{document}

Best Answer

Update:

The notes have been requested to be inside the story box. Now I offer two options, both of them with automatic numbering for the notes and with the possibility to cross-reference them:

  1. In this first option, the box for the story has a width equal to \textwidth, but the story text goes to the left of the box; the notes appear to the left of the text:

    \documentclass[dvipsnames]{article}
    \usepackage[lmargin=2.5cm,textwidth=13cm,marginparwidth=5cm]{geometry}
    \usepackage[many]{tcolorbox}
    \usepackage{tikzpagenodes}
    \usetikzlibrary{tikzmark}
    \usepackage{lipsum}
    
    \newtcolorbox{story}[1][]{
      width=\textwidth,
      fonttitle=\bfseries,
      breakable,
      extrude right by=4cm,
      fonttitle=\bfseries\color{Brown},
      colframe=Melon,
      colback=Melon!10
      #1}
    
    \newcounter{mynote}
    \newtcolorbox[use counter=mynote]
      {mynote}[1][]
      {title=Note~\thetcbcounter,
       width=4cm,
       left=0pt,
       right=0pt,
       fonttitle=\bfseries,
       coltitle=black,
       colframe=ForestGreen!40,
       colback=ForestGreen!10,
       #1
    }    
    
    \newcounter{tmp}
    \newcommand\StoryNote[3][]{%
        \stepcounter{tmp}%
        \tikzmark{mark-\thetmp}%
        \begin{tikzpicture}[remember picture,overlay]
        \node[inner sep=0pt,anchor=north west] 
          at ([xshift=23pt]current page text area.east|-{pic cs:mark-\thetmp})
          {\begin{mynote}[label=#3]
            #2
          \end{mynote}};
        \end{tikzpicture}%
    }
    
    \begin{document}
    \lipsum*[2] As we can see in notes~\ref{testa} and \ref{testb}...
    \begin{story}
    Some text\StoryNote{Some text here to illustrate the box for the notes.}{testa} for demo.
    \lipsum*[3]\StoryNote{Some text here to illustrate the box for the notes.}{testb}
    \lipsum*[3]
    \end{story}
    
    \end{document}
    

    enter image description here

  2. In this second option, the notes are placed using wrapfigure from the wrapfig package, so that the story text wraps around the note text; this approach, however, must be carefully used due to restrictions (imposed by wrapfig); in particular, one must guarantee that a note won't appear near a page break, notes can't be used inside lists.

    \documentclass[dvipsnames]{article}
    \usepackage[lmargin=2.5cm,textwidth=13cm,marginparwidth=5cm]{geometry}
    \usepackage[many]{tcolorbox}
    \usepackage{wrapfig}
    \usepackage{lipsum}
    
    \newtcolorbox{story}[1][]{
      width=\textwidth,
      fonttitle=\bfseries,
      breakable,
      fonttitle=\bfseries\color{Brown},
      colframe=Melon,
      colback=Melon!10
      #1}
    
    \newcounter{mynote}
    \newtcolorbox[use counter=mynote]
      {mynote}[1][]
      {title=Note~\thetcbcounter,
       width=4cm,
       left=0pt,
       right=0pt,
       fonttitle=\bfseries,
       coltitle=black,
       colframe=ForestGreen!40,
       colback=ForestGreen!10,
       #1
    }    
    
    \newcommand\StoryNote[3][]{%
         \begin{wrapfigure}{r}{4cm}
          \begin{mynote}[label=#3]
            #2
          \end{mynote}%
          \end{wrapfigure}%
    }
    
    \begin{document}
    \lipsum*[2] As we can see in notes~\ref{testa} and \ref{testb}...
    \begin{story}
    \StoryNote{Some text here to illustrate the box for the notes.}{testa}
    \lipsum[1]
    \StoryNote{Some text here to illustrate the box for the notes.}{testb}
    \lipsum[3]
    \end{story}
    
    \end{document}
    

    enter image description here

Initial version:

Here's another option giving you automatic numbering for the notes and possibility to cross-reference them as required:

\documentclass[dvipsnames]{article}
\usepackage[lmargin=2.5cm,textwidth=13cm,marginparwidth=5cm]{geometry}
\usepackage[many]{tcolorbox}
\usepackage{marginnote}
\usepackage{lipsum}

\newtcolorbox{story}[1][]{
  width=\textwidth,
  fonttitle=\bfseries,
  breakable,
  fonttitle=\bfseries\color{Brown},
  colframe=Melon,
  colback=Melon!10
  #1}

\newcounter{mynote}
\newtcolorbox[use counter=mynote]
  {mynote}[1][]
  {title=Note~\thetcbcounter,
   width=5cm,
   left=0pt,
   right=0pt,
   fonttitle=\bfseries\color{Brown},
   colframe=Melon,
   colback=Melon!10,
   #1
}    

\newcommand\StoryNote[3][]{%
  \marginnote[#1]{%
    \makebox[0pt][l]{\begin{mynote}[label=#3]
    #2
    \end{mynote}}}%
}

\begin{document}
\lipsum*[2] As we can see in notes~\ref{testa} and \ref{testb}...
\begin{story}
Some text\StoryNote{Some text here to illustrate the box for the notes.}{testa} for demo.
\lipsum*[3]\StoryNote{Some text here to illustrate the box for the notes.}{testb}
\lipsum*[3]
\end{story}

\end{document}

enter image description here

Remarks:

  1. You write the text for the story inside the story environment (it has an optional argument allowing you to pass options (such as an eventual tile) to the tcolorbox used.

  2. You write the notes using the \StoryNote command with syntax

    \SoryNote{<note text>}{<label>} 
    

    where is the text for the note itself and is the string that can be used to produce cross-references (see example code above). The notes are automatically numbered.