TeX 命令进入环境

TeX 命令进入环境

如何将下面提到的 Latex“命令”更改为“环境”模式

例如

\marginnotesone{sample text}

--->

\begin{marginnotesone}sample text\end{marginnotesone}

请查看我的 tex 代码如下:

\documentclass{book}
\usepackage{xcolor}

\newcommand\marginnotesone[1]{%
\marginpar{%
\fontsize{9.5}{12}\selectfont\raggedright\textcolor{cyan}{#1}}
}

\begin{document}

\marginnotesone{The analyst must know what information is really needed, and
obtain a representative sample, pp. 6, 9}
The analyst must know what information is really needed, and
obtain a representative sample, pp. 6, 9
The analyst must know what information is really needed, and
obtain a representative sample, pp. 6, 9
The analyst must know what information is really needed, and
obtain a representative sample, pp. 6, 9
The analyst must know what information is really needed, and
obtain a representative sample, pp. 6, 9

\end{document}

答案1

您可以使用xparse,它比具有更多的功能environ并且不需要\BODY

我还修复了建议代码中的几个缺陷:\leavevmode进入环境时发出有助于对齐边注(如果边注位于段落开头)。在内部发出\marginpar时,它可以避免出现虚假的线条。\color{cyan}

\documentclass{book}
\usepackage{xcolor}
\usepackage{xparse}

\NewDocumentEnvironment{marginnotesone}{+b}
 {%
  \leavevmode
  \marginpar{%
    \leavevmode\fontsize{9.5}{12}\selectfont\raggedright\color{cyan}#1%
  }%
 }%
 {}

\begin{document}

\begin{marginnotesone}
The analyst must know what information is really needed, and
obtain a representative sample, pp. 6, 9
\end{marginnotesone}
The analyst must know what information is really needed, and
obtain a representative sample, pp. 6, 9
The analyst must know what information is really needed, and
obtain a representative sample, pp. 6, 9
The analyst must know what information is really needed, and
obtain a representative sample, pp. 6, 9
The analyst must know what information is really needed, and
obtain a representative sample, pp. 6, 9

\end{document}

在此处输入图片描述

答案2

您可以使用environ包来实现这一点。然后您只需用 替换#1即可\BODY

\documentclass{book}
\usepackage{xcolor}
\usepackage{environ}
\NewEnviron{marginnotesone}{\marginpar{%
\fontsize{9.5}{12}\selectfont\raggedright\textcolor{cyan}{\BODY}}}


\begin{document}

\begin{marginnotesone}
The analyst must know what information is really needed, and
obtain a representative sample, pp. 6, 9
\end{marginnotesone}
The analyst must know what information is really needed, and
obtain a representative sample, pp. 6, 9
The analyst must know what information is really needed, and
obtain a representative sample, pp. 6, 9
The analyst must know what information is really needed, and
obtain a representative sample, pp. 6, 9
The analyst must know what information is really needed, and
obtain a representative sample, pp. 6, 9

\end{document}

在此处输入图片描述

相关内容