Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

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.

share|improve this answer
    
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
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.