我正在尝试编写一个简单的宏,它将添加一个旁注,如果将旁注添加到标题中,它将内联注释。为此,我正在测试我是否处于内部模式,但测试似乎不起作用。有人能建议我该怎么做吗?以下是失败代码的示例。关键是我\addnote
在第 9 行的定义,它应该添加旁注或仅添加文本。
\documentclass[12pt]{article}
\usepackage{sidenotes}
\usepackage{setspace}
\usepackage{color}
\def\mysidenote#1#2{\color{#1}\marginpar{\footnotesize\setstretch{0.9}\raggedright\color{#1} #2}}
\def\addnote#1#2{\ifinner{\color{#1} #2}\else \mysidenote{#1}{#2} \fi}
%\def\addnote#1#2{\mysidenote{#1}{#2}}
\usepackage[total={6.5in,8.5in},top=1in,left=.75in,marginparwidth=2.5in,, marginparsep=.1in,includeall]{geometry}
\begin{document}
here is some text\addnote{red}{the first note} and some more text\addnote{blue}{the second note} and blah blah.
\begin{figure}
a figure
\caption{figure caption\addnote{green}{note in caption}}
\end{figure}
and some more text after the figure.
\end{document}
当上述代码通过 pdflatex 运行时,我得到:
! LaTeX Error: Float(s) lost.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.20 \end{document}
请注意,如果我不将\ifinner
测试放在的定义中\addnote
,那么我会得到:
! LaTeX Error: Not in outer par mode.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.17 ...e caption\addnote{green}{note in caption}}
所以,测试确实在做一些事情,但不是我想要它做的事情。欢迎提出任何建议。
答案1
测试来自这里似乎有效。
\documentclass[12pt]{article}
\usepackage{sidenotes}
\usepackage{setspace}
\usepackage{xcolor}
\makeatletter
\newcommand\ifInFloat[2]{\@ifundefined{@captype}{#2}{#1}}
\makeatother
\def\mysidenote#1#2{\color{#1}\marginpar{\footnotesize\setstretch{0.9}\raggedright\color{#1} #2}}
\def\addnote#1#2{\ifInFloat{\color{#1} #2}{\mysidenote{#1}{#2}}}
\usepackage[total={6.5in,8.5in},top=1in,left=.75in,marginparwidth=2.5in,, marginparsep=.1in,includeall]{geometry}
\begin{document}
here is some text\addnote{red}{the first note} and some more text\addnote{blue}{the second note} and blah blah.
\begin{figure}
a figure
\caption{figure caption
\protect\addnote{green}{note in caption}
}
\end{figure}
and some more text after the figure.
\end{document}