4

Using a box technique (Paragraph numbers at left margin), I added margin text.

I'd like the top of the left-aligned number to be flush with the top of the paragraph text rather than having the baselines aligned.

I am hoping to learn a box trick here. (I can do this with tikz, but I'd like to learn how to do it with TeX or LaTeX boxes) I tried adding a third \makebox[0pt][t]{1.} but I could not get it to align as desired. Is minipage the only/best way? My skills got a little weak after the Christmas vacation :=)

Code

\documentclass{article}
\usepackage{fontspec}% xelatex
\usepackage{lipsum}
\setlength{\parindent}{0mm}
\newcommand{\marginnum}[1]{\makebox[0mm][r]{\makebox[12mm][l]{\Huge\bfseries #1.}}}
\begin{document}
\marginnum{1}\lipsum[1]
\end{document}

Output

enter image description here

Desired Output using TikZ

Using a similar \makebox of zero-width technique, I create an inline node on the first line of the paragraph. I then anchor a second node to the top of that node containing my margin text.

\documentclass{article}
\usepackage{fontspec}% xelatex
\usepackage{lipsum}
\usepackage{tikz}\usetikzlibrary{calc}
\setlength{\parindent}{0mm}
\newcommand{\marginnum}[1]{%
\makebox[0pt][r]{%
\begin{tikzpicture}[remember picture]%
\node [inner sep=0pt,outer sep=0pt] (parstart) {\phantom{L}};%
\end{tikzpicture}}%
\begin{tikzpicture}[remember picture, overlay]%
\node [inner sep=0pt,outer sep=0pt,anchor=north east,font=\bfseries\Huge] at ($ (parstart.north west)+(-12mm,0) $) {#1.};%
\end{tikzpicture}%
}%
\begin{document}
\marginnum{1}\lipsum[1]
\end{document}

enter image description here

3

Raise it into position:

enter image description here

\documentclass{article}

\usepackage{lipsum}

\setlength{\parindent}{0mm}

\newcommand{\marginnum}[1]{%
  \makebox[0mm][r]{%
    \makebox[12mm][l]{%
      \raisebox{\dimexpr-\height+.6\baselineskip}[0pt][0pt]{\Huge\bfseries #1.}}}}

\begin{document}

\marginnum{1}\lipsum[1]

\end{document}

A more exact method for the vertical position would be to use identify the height of some capital letter in the current font, and raise the number into position relative to that:

\newcommand{\marginnum}[1]{%
  \makebox[0mm][r]{%
    \makebox[12mm][l]{%
      \raisebox{\dimexpr-\height+\fontcharht\font`A}[0pt][0pt]{\Huge\bfseries #1.}}}}
  • How did you choose the .6\baselineskip? I was considering to use calc's \heightof command. – jakun Jan 4 '17 at 17:51
  • @jakun: Yes, one can perform a specific calculation using \settoheight (no calc needed) to obtain the exact height. A ( is typically used to measure the height of a rule in the form of a \strut, so this depends on the height of the tallest element in the line. In this case though, I just eye-balled it. – Werner Jan 4 '17 at 17:54
  • And will is this scalable? What happens if I decide to change the paragraph font size. Is there any way to replace .6 with something relative to the font size itself? Oh I guess jakun beat me to the punch. – Jonathan Komar Jan 4 '17 at 17:55
  • @macmadness86: The .6 is relative to the font size since it uses \baselineskip. Try it with \Large\marginnum{1}\lipsum[1]. – Werner Jan 4 '17 at 17:56
  • 1
    @macmadness86: I've added an updated \marginnum that measures the height of some capital letter and places the number relative to that. – Werner Jan 4 '17 at 18:08
4

Set the object in a “top” \parbox with the reference point established with a zero height object and raise it the height of a capital letter.

\documentclass{article}
\usepackage{lipsum}

\newlength{\marginnumwidth}
\setlength{\marginnumwidth}{12mm}

\newcommand{\marginnum}[1]{%
  \par\noindent
  \hspace*{-\marginnumwidth}%
  \raisebox{\fontcharht\font`A}[0pt][0pt]{%
    \parbox[t]{\marginnumwidth}{\vspace*{0pt}\Huge #1.}%
  }%
  \ignorespaces
}

\begin{document}

\marginnum{1}
\lipsum[1]

\end{document}

enter image description here

  • Why do you use \ignorespaces here? I seem to be having a space issue (in my actual document, not my minimal example) – Jonathan Komar Jan 5 '17 at 6:05
  • @macmadness86 So I don't need to worry about having \marginnum on a line by itself – egreg Jan 5 '17 at 9:59
  • I think making \marginnumwidth dynamic is more robust. For future reference, you did it here: tex.stackexchange.com/questions/312581/… – Jonathan Komar Feb 13 '17 at 21:13
4

As I had almost the same solution as Werner, I'll add another solution based on the lettrine package. The number font size is slighly larger since base line of the number aligns with the second line of text by default:

\documentclass{article}
\usepackage{fontspec}% xelatex
\usepackage{lettrine} \usepackage{lipsum}
\setlength{\parindent}{0mm}
\newcommand{\marginnum}[1]{\makebox[0mm][r]{\raisebox{\dimexpr-\height +1.55ex\relax}[0pt][0pt]{\makebox[12mm][l]{\Huge\bfseries #1.}}}}

\newcommand{\varmarginnum}[1]{\lettrine[lhang=1, nindent=0pt]{\bfseries\makebox[12mm][l]{1.}}{}}

\begin{document}

\marginnum{1}\lipsum[1]

\varmarginnum{1}\lipsum[1]

\end{document} 

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.