4

Someone wrote a command called \MarginFigure for putting figures in the page margins. Unfortunately, I haven't been able to get the \includegraphics to work.

Specifically, I have figures that I've created using \newcommand and then drawing a tikzpicture. When I attempt to replace the example image with my new image, it doesn't work. Here is a minimal working example to show you what I'm talking about:

\documentclass[oneside]{amsart}
\usepackage{amsmath,amssymb}
\usepackage[T1]{fontenc}
\usepackage{concmath}
\usepackage[rmargin=3cm,textwidth=11cm,marginparwidth=6cm]{geometry}
\usepackage{graphicx}
\usepackage{marginnote}
\usepackage{caption}
\usepackage{tikz}
\usepgflibrary{shapes}
\usepackage{pgfplots}
\usepackage{lipsum}

\newcommand\MarginFigure[4][width=4cm]{
\marginnote{
\begin{minipage}{\linewidth}
    \centering
    \includegraphics[#1]{#2}
    \captionof{figure}{#3}
    \label{#4}
\end{minipage}}}
\reversemarginpar

\pgfmathsetmacro{\ex}{0}
\pgfmathsetmacro{\ey}{0}

\newcommand{\figureone}[1]{
\begin{tikzpicture}[#1]
        \draw [fill = lightgray] (0,0) circle [radius = 1.25 cm];
    \draw (0,0) -- (0.8838834765,0.8838834765);
    \node [above left] at (0.5,0.5) {$r$};
        \node [above left] at (-1.0,0.575) {$C$};
    \draw [->] (\ex,\ey) ++(135:1.5cm) arc (135:85:1.5cm);
    \draw [->] (\ex,\ey) ++(160:1.5cm) arc (160:210:1.5cm);
    \end{tikzpicture}
}

\begin{document}
\lipsum[1-2]{}
\MarginFigure{example-image-a}{The Area of a Disk}{fig:testa}
\end{document}

It works perfectly the way it is, but when I replace example-image-a with \figureone, it won't compile. I'm not sure why. Thanks for your help.

  • The first argument to \MarginFigure is passed to \includegraphics as the file name. – egreg Sep 26 '13 at 23:33
  • Thank you for upvoting my question and the someone is me. – kiss my armpit Sep 27 '13 at 4:51
4

As egreg points out in the comment,

The first argument to \MarginFigure is passed to \includegraphics as the file name.

Unfortunately \includegraphics expects a figure file in the form of .pdf or .png etc. But here is a work around. You can save your tikzpicture as a separate file: figureone.tikz and then use tikzscale package. tikzscale allows us to input .tikz file to \includegraphics command (\includegraphics{figureone.tikz}):

\documentclass[oneside]{amsart}
\usepackage{amsmath,amssymb}
\usepackage[T1]{fontenc}
\usepackage{concmath}
\usepackage[rmargin=3cm,textwidth=11cm,marginparwidth=6cm]{geometry}
\usepackage{graphicx}
\usepackage{marginnote}
\usepackage{caption}
\usepackage{tikz}
\usepgflibrary{shapes}
\usepackage{pgfplots}
\usepackage{lipsum}
\usepackage{tikzscale}

\newcommand\MarginFigure[4][width=4cm]{
\marginnote{
\begin{minipage}{\linewidth}
    \centering
    \includegraphics[#1]{#2}
    \captionof{figure}{#3}
    \label{#4}
\end{minipage}}}
\reversemarginpar

\pgfmathsetmacro{\ex}{0}
\pgfmathsetmacro{\ey}{0}

\usepackage{filecontents}
\begin{filecontents*}{figureone.tikz}
\begin{tikzpicture}%% [#1]
        \draw [fill = lightgray] (0,0) circle [radius = 1.25 cm];
    \draw (0,0) -- (0.8838834765,0.8838834765);
    \node [above left] at (0.5,0.5) {$r$};
        \node [above left] at (-1.0,0.575) {$C$};
    \draw [->] (\ex,\ey) ++(135:1.5cm) arc (135:85:1.5cm);
    \draw [->] (\ex,\ey) ++(160:1.5cm) arc (160:210:1.5cm);
    \end{tikzpicture}
\end{filecontents*}

\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus
elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum
gravida mauris. Nam arcu libero, nonummy eget, consectetuer id, vulpu-
tate a, magna. Donec vehicula augue eu neque. Pellentesque habitant
morbi tristique senectus et netus et malesuada fames ac turpis egestas.
Mauris ut leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibu-
lum urna fringilla ultrices. Phasellus eu tellus sit amet tortor gravida
placerat. Integer sapien est, iaculis in, pretium quis, viverra ac, nunc.
Praesent eget sem vel leo ultrices bibendum. Aenean faucibus. Morbi
dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. \MarginFigure{figureone.tikz}{The Area of a Disk}{fig:testa} Curabitur auc-
tor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue
eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci
dignissim rutrum.

Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi.
Morbi auctor lorem non justo. Nam lacus libero, pretium at, lobortis
vitae, ultricies et, tellus. Donec aliquet, tortor sed accumsan bibendum,
erat ligula aliquet magna, vitae ornare odio metus a mi. Morbi ac orci et
nisl hendrerit mollis. Suspendisse ut massa. Cras nec ante. Pellentesque
a nulla. Cum sociis natoque penatibus et magnis dis parturient montes,
nascetur ridiculus mus. Aliquam tincidunt urna. Nulla ullamcorper
vestibulum turpis. Pellentesque cursus luctus mauris.

\end{document}

enter image description here

  • Or define a different command for TikZ pictures. – egreg Sep 27 '13 at 0:32
  • @egreg That is also an option which I would have adopted :) – user11232 Sep 27 '13 at 0:36
  • One more thing: Is it possible to control the vertical positioning of these margin figures, so that I can put them next to the corresponding text? – Randy Randerson Sep 27 '13 at 3:39
  • 1
    @fctaylor25 It is actually next to the corresponding text. To check, use some actual text (the content of lipsum[21-2]) instead of lipsum[1-2]. See the edit. – user11232 Sep 27 '13 at 4:08
  • 1
    @fctaylor25 In worst scenarios, you can put \vspace{-<dimen>} just before \begin{minipage} – user11232 Sep 27 '13 at 4:33
1

This solution relies on placing the image inside a \sbox, and then placing the \usebox in the margin.

\documentclass[oneside]{amsart}
\usepackage{amsmath,amssymb}
\usepackage[T1]{fontenc}
\usepackage{concmath}
\usepackage[rmargin=3cm,textwidth=11cm,marginparwidth=6cm]{geometry}
\usepackage{graphicx}
\usepackage{marginnote}
\usepackage{caption}
\usepackage{tikz}
\usepgflibrary{shapes}
\usepackage{pgfplots}
\usepackage{lipsum}
\newsavebox{\myfig}
\newcommand\MarginFigure[4][width=4cm]{%
\sbox{\myfig}{#2}
\marginnote{
\begin{minipage}{\linewidth}
    \centering
    \usebox{\myfig}
    \captionof{figure}{#3}
    \label{#4}
\end{minipage}}%
}
\reversemarginpar

\pgfmathsetmacro{\ex}{0}
\pgfmathsetmacro{\ey}{0}

\newcommand\figureone[1][]{%
\begin{tikzpicture}[#1]
        \draw [fill = lightgray] (0,0) circle [radius = 1.25 cm];
    \draw (0,0) -- (0.8838834765,0.8838834765);
    \node [above left] at (0.5,0.5) {$r$};
        \node [above left] at (-1.0,0.575) {$C$};
    \draw [->] (\ex,\ey) ++(135:1.5cm) arc (135:85:1.5cm);
    \draw [->] (\ex,\ey) ++(160:1.5cm) arc (160:210:1.5cm);
    \end{tikzpicture}%
}

\begin{document}
\lipsum[1-2]{}
\MarginFigure{\figureone}{The Area of a Disk}{fig:testa}
\end{document}

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.