8

I often use \marginpar{} which is boring to me. Is there a way to add grey background and round black edges so it would look like this:

Wanted marginpar

71GA
  • 3,573

1 Answers1

8

1st solution, just using tikz

Here's a solution that renews the \marginpar command and uses TikZ to provide very flexible decorations

enter image description here

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}

\let\oldmarginpar\marginpar
% renew the \marginpar command to draw 
% a node; it has a default setting which 
% can be overwritten
\renewcommand{\marginpar}[2][rectangle,draw,fill=orange,rounded corners]{%
        \oldmarginpar{%
        \tikz \node at (0,0) [#1]{#2};}%
        }

\begin{document}
\marginpar{margin text here}
\lipsum[1]

\marginpar[fill=red,text=yellow,circle]{margin text here}
\lipsum[2]
\end{document}

2nd solution, using mdframed

This one uses the mdframed package for the framing, and defines an environment rather than a command. I've copied some code from the tufte document class to define an environment mymarginpar

screenshot

\documentclass{article}
\usepackage[xcolor]{mdframed}
\usepackage{lipsum}
%====================================
%   shamelessly copied from tufte documentclass
%====================================
\makeatletter
% Margin float environment
\newsavebox{\@my@margin@floatbox}
\newenvironment{@my@margin@float}[1][-1.2ex]%
{%
\begin{lrbox}{\@my@margin@floatbox}%
  \begin{minipage}{\marginparwidth}%
    \hbox{}\vspace*{#1}%
    \noindent%
    }
    {\end{minipage}%
  \end{lrbox}%
  \marginpar{\usebox{\@my@margin@floatbox}}%
  }

% marginpar environment
\newenvironment{mymarginpar}[1][-1.2ex]%
{\begin{@my@margin@float}[#1]%
  \begin{mdframed}[backgroundcolor=black!30,linecolor=red]%
 }%
 {\end{mdframed}\end{@my@margin@float}}
\makeatother

\begin{document}
\begin{mymarginpar}
  margin text here
\end{mymarginpar}
\lipsum
\end{document}

If you want rounded corners, you can investigate the tikz-method of the mdframed, see the documentation for details.

cmhughes
  • 100,947
  • Nice to see my package ;-) However I don't know whether a standard TikZ implementation is easier. – Marco Daniel Aug 23 '12 at 18:47
  • This example you provided returns an error: "! LaTeX Error: Command \marginpar already defined." – 71GA Aug 23 '12 at 18:58
  • @71GA not for me- are you sure you're using it as-is? Or have you tried to replace mymarginpar with marginpar? – cmhughes Aug 23 '12 at 19:01
  • I am sure tried it once more. It doesnt work. I literally copied your example. – 71GA Aug 23 '12 at 21:03
  • @71GA not sure what's going on, perhaps an update is in order. Meanwhile, I've added another solution that uses tikz, perhaps you'll prefer this anyway :) – cmhughes Aug 23 '12 at 22:14
  • Well as it showed up, your MWE is working on it self, but it hangs when i try to implement it in my document and then build. – 71GA Aug 24 '12 at 00:00
  • @71GA if you have an issue in your actual document, try and strip it down to a MWE and post another question :) – cmhughes Aug 24 '12 at 04:23
  • 1
    add \node[text width=1in] to the Tikz if you want margin to wrap – Algeboy Aug 10 '23 at 12:54