4

How to change below mentioned latex "command" as "environment" mode

e.g.

\marginnotesone{sample text}

--->

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

Please find my tex code as below:

\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
  • Off-topic: the soace after "pp." is being interpreted as an end-of-sentence space on account of the period. Two possible fixes: if aline shoiuldn't be broken at that point, use "pp.~6"; if line-breaking doesn't matter, input "pp.\ 6". Jan 14, 2020 at 17:23

2 Answers 2

7

You can use xparse, which has many more features than environ and doesn't need \BODY.

I also fixed a few weaknesses in the proposed code: issuing \leavevmode upon entering the environment helps in getting the margin note aligned in case it is at the beginning of a paragraph. Inside the \marginpar it avoids a spurious line when \color{cyan} is issued.

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

enter image description here

6

You can use the environ package for that. Then you merely need to replace #1 by \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}

enter image description here

2
  • Thank you very much, this is I was expected result.
    – Rajesh N
    Jan 13, 2020 at 4:57
  • @RajeshN You are welcome!
    – user194703
    Jan 13, 2020 at 4:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .