17

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}
  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – cfr May 20 '14 at 2:52
16

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.

| improve this answer | |
  • for both answers accounts that the margin note is not really a part of the story. I would like to have the textwidth of the story smaller so that in the coloured box the margin note could be placed, just like the footnotes are part of the story. – Avm May 20 '14 at 13:22
  • @Avm Please see my updated answer. – Gonzalo Medina May 20 '14 at 14:20
  • @Avm I added another option to my updated answer. – Gonzalo Medina May 20 '14 at 15:30
  • Thank's to Gonzalo Medina, this is it, the geometry package does it and now I even know how I have to use it. Helped a lot. – Avm May 20 '14 at 17:36
3

Change colours as you wish and see the documentation for more options in tcolorbox. This should serve as a starter.

\documentclass[12pt]{article}
\usepackage[many]{tcolorbox}
\usepackage{marginnote}
\usepackage{kantlipsum}
\tcbuselibrary{skins,breakable}
\newtcolorbox{story}[1][]{
    width=\textwidth,
    colback=magenta!20,
    colframe=red!75!black,
    colbacktitle=blue!85!black,
    fonttitle=\bfseries,
    left=0ex,
    right=0ex,
    top=0pt,
    arc=0pt,
    outer arc=0pt,
    leftrule=0pt,
    rightrule=0pt,
    toprule=0pt,
    bottomrule=0pt,
    breakable,
    enhanced jigsaw,
    title= #1}

\begin{document}
\kant[2]
\begin{story}[Some story]
Some text comes here just\marginnote{some note} for demo.
\kant[3]
\end{story}
\end{document}

enter image description here

| improve this answer | |
  • How can we have just margin notes on left without textboxes, just simple notes on left side using marginnote package? – user24203 May 5 '17 at 7:47
  • @user24203 If I understand your question correct, this would be another question not just a comment. Nevertheless, search for \reversemarginpar in the marginnote manual. – Schweinebacke May 5 '17 at 8:16

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.