2

Trying to figure out how they place the command names on the left margin on package docs on CTAN, e.g.

enter image description here

I have tried using marginpar, but my text is not properly aligned. Minimal example:

\documentclass{article}

\usepackage[a4paper,left=6cm,top=2cm,right=2cm,bottom=2cm]{geometry} \usepackage{marginnote} \usepackage{lipsum}

\begin{document}

\reversemarginpar\marginpar{\raggedright SomeCommand}% \lipsum[1]

\end{document}

Result:

enter image description here

Obviously, the text on the left is not aligned to the first line of the paragraph, which is the desired effect. I am also interested in having the text on the left to use \ttfamily.

How should I proceed?

Victor
  • 367
  • many packages use the doc package (or ltxdoc class which uses doc.) The source for every document is available at the same place (minted.dtx in this example) – David Carlisle Jun 26 '22 at 22:17
  • I should've probably mentioned I am not documenting a Latex package, but an API of mine. I noticed the doc package exposes things strictly related to documenting other Latex packages. Is that the way to go for me? – Victor Jun 26 '22 at 22:39
  • well doc is almost certainly the answer of the question you asked, but it may not be the answer for your real use case as it is rather specialised for tex packages. You may prefer a basic description list and use enumitem package to adjust the layout to your requirements – David Carlisle Jun 26 '22 at 22:44
  • i might be just dumb, but can't seem to figure it out. would you be able to provide an example, please? – Victor Jun 26 '22 at 23:12
  • Use the DescribeMacro macro. Search for an example in any dtx file (or in this unrelated answer of mine for an example that is usable in "normal TeX files" i.e. one that doesn't "comment out every lines". – user202729 Jun 27 '22 at 00:30
  • You may also want to have a look at the documentation library of tcolorbox. – frougon Jun 27 '22 at 12:53

2 Answers2

5

The alignment is messed up because marginpar is being invoked in vertical mode. A \leavevmode remedies the issue. As to \ttfamily, it can just be added to the marginpar.

\documentclass{article}

\usepackage[a4paper,left=6cm,top=2cm,right=2cm,bottom=2cm]{geometry} \usepackage{marginnote} \usepackage{lipsum}

\begin{document}

\leavevmode\reversemarginpar\marginpar{\raggedright\ttfamily SomeCommand}% \lipsum[1]

\end{document}

enter image description here

If you wish to get the backslash for a command name in such a context, just use the \char'134 primitive, as in

\leavevmode\reversemarginpar\marginpar{\raggedright\ttfamily\char'134
 SomeCommand}
  • \char`\ should work as well (although it's nearly impossible to type correctly in one of these comments). – barbara beeton Jun 27 '22 at 03:02
  • @barbarabeeton Just type ​`​`\char`\\`​​`​ to get \char`\\. – user202729 Jun 27 '22 at 04:25
  • @user202729 -- I'm pretty sure I did try that, with incorrect results, but maybe I fumbled something. Anyhow, thanks. (I prefer this formulation because it's lot's easier to remember than a number.) – barbara beeton Jun 27 '22 at 13:52
4

To answer the question in the title, use DescribeMacro. (see the documentation source code for any package to see how it's done. While it doesn't start with a backslash, it appears to work here.)

%! TEX program = pdflatex
\documentclass{article}

\usepackage{geometry} \usepackage{doc} \usepackage{lipsum}

\begin{document}

\DescribeMacro{SomeCommand} \lipsum[1]

\end{document}

An image of the output isn't very important, but it's below.

image of the output

user202729
  • 7,143