使用 \MarginFigure 命令

使用 \MarginFigure 命令

有人写了一个命令,要求\MarginFigure将数字放在页边距中。不幸的是,我无法让它工作\includegraphics

具体来说,我使用\newcommand绘制了一些图形tikzpicture。当我尝试用我的新图像替换示例图像时,它不起作用。这是一个最小的工作示例,向您展示我在说什么:

\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}

它本来就运行得很好,但是当我example-image-a用替换时\figureone,它就无法编译。我不知道为什么。谢谢你的帮助。

答案1

正如 egreg 在评论中指出的那样,

第一个参数作为文件名\MarginFigure传递给。\includegraphics

不幸的是,\includegraphics需要一个图形文件,格式为.pdf.png等。但这里有一个解决方法。您可以将您的另存tikzpicture为单独的文件:figureone.tikz然后使用tikzscale包。tikzscale允许我们将.tikz文件输入到\includegraphics命令(\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}

在此处输入图片描述

答案2

此解决方案依赖于将图像放置在 内部\sbox,然后将 放置\usebox在边缘。

\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}

相关内容