Guide LaTeX document authoring following best practices and proper semantic markup...
This skill guides the creation of well-structured, semantically correct LaTeX documents following established best practices.
Use LaTeX environments that match the semantic meaning of the content, not just the visual appearance.
description for Term-Definition PairsWhen you have labels followed by explanations, definitions, or descriptions, use the description environment:
\begin{description}
\item[Term] Definition or explanation of the term
\item[Label] Content associated with the label
\item[Property] Description of the property
\end{description}
NEVER do this:
\begin{itemize}
\item \textbf{Term:} Definition or explanation
\item \textbf{Label:} Content associated with label
\end{itemize}
description\item[username] The user's login name\item[timeout] Maximum wait time in seconds\item[LaTeX] A document preparation system\item[Passes] Correct implementation...\item[Auto-save] Automatically saves every 5 minutesitemize for Simple ListsUse itemize when items are uniform list elements without labels:
\begin{itemize}
\item First uniform item
\item Second uniform item
\item Third uniform item
\end{itemize}
enumerate for Numbered Steps or RankingsUse enumerate when order matters:
\begin{enumerate}
\item First step in the process
\item Second step in the process
\item Third step in the process
\end{enumerate}
When reviewing or writing LaTeX, look for these patterns that indicate description should be used:
\item \textbf{SomeLabel:} → Should use \item[SomeLabel]\item \emph{SomeLabel:} → Should use \item[SomeLabel]\item SomeLabel --- → Should use \item[SomeLabel]% INCORRECT
\begin{itemize}
\item \textbf{Passes:} \verb|\documentclass{article}|
\item \textbf{Fails:} No documentclass declaration
\end{itemize}
% CORRECT
\begin{description}
\item[Passes] \verb|\documentclass{article}|
\item[Fails] No documentclass declaration
\end{description}
% INCORRECT
\noindent\textbf{Configuration:} Set timeout to 30 seconds.\\
\textbf{Performance:} Optimized for large datasets.
% CORRECT
\begin{description}
\item[Configuration] Set timeout to 30 seconds
\item[Performance] Optimized for large datasets
\end{description}
CRITICAL: When writing LaTeX in literate programming files (.nw), use noweb's [[code]] notation for quoting code, not \texttt with manual escaping.
[[code]] Notation, Not \texttt{...\_...}The noweb literate programming system provides special notation for code references that automatically handles special characters like underscores.
Anti-pattern: Manual underscore escaping with \texttt
% INCORRECT - in .nw files
The \texttt{get\_submission()} method calls \texttt{\_\_getattribute\_\_}.
We store \texttt{\_original\_get\_submission} in the closure.
The \texttt{NOREFRESH\_GRADES} constant defines final grades.
Correct: Use [[code]] notation
% CORRECT - in .nw files
The [[get_submission()]] method calls [[__getattribute__]].
We store [[_original_get_submission]] in the closure.
The [[NOREFRESH_GRADES]] constant defines final grades.
Why this matters:
[[code]] vs \textttUse [[code]] in .nw files for:
[[get_submissions()]], [[__init__]][[_includes]], [[user_id]][[LazySubmission]], [[Assignment]][[NOREFRESH_GRADES]], [[MAX_RETRIES]][[pickle]], [[Canvas]]Use \texttt in .nw files for:
\texttt{.py}, \texttt{.nw}In regular .tex files (not literate programs):
\texttt with proper escaping as [[...]] is not availableminted or listings for codeWhen reviewing .nw files, look for these anti-patterns:
\texttt{..._...} → Should use [[...]]\texttt{...__...} → Should use [[...]]\item[SOME\_CONSTANT behavior] → Should use better label + [[SOME_CONSTANT]] in textDocumenting methods:
% INCORRECT
The \texttt{\_\_getstate\_\_} method excludes \texttt{\_original\_get\_submission}.
% CORRECT
The [[__getstate__]] method excludes [[_original_get_submission]].
Documenting constants:
% INCORRECT
\item[NOREFRESH\_GRADES behavior] Submissions with final grades...
% CORRECT
\item[Final grade policy] Submissions with final grades (A, P, P+, complete)
are never refreshed, maintaining the [[NOREFRESH_GRADES]] policy.
Documenting attributes:
% INCORRECT
The decorator adds a \texttt{\_\_cache} dictionary and \texttt{\_\_all\_fetched} flag.
% CORRECT
The decorator adds a [[__cache]] dictionary and [[__all_fetched]] flag.
\cref{...} (cleveref package) for all cross-references\S\ref{...} or manually type section/figure prefixes\label{sec:introduction} not \label{s1}\cref{sec:background} → "Section 2.1"\cref{fig:diagram} → "Figure 3"\cref{tab:results} → "Table 1"\cref{sec:intro,sec:conclusion} → "Sections 1 and 4"Anti-pattern: Manual prefixes
% INCORRECT
Section~\ref{sec:intro} shows...
\S\ref{sec:background} discusses...
Figure~\ref{fig:plot} demonstrates...
% CORRECT
\cref{sec:intro} shows...
\cref{sec:background} discusses...
\cref{fig:plot} demonstrates...
Why: The cleveref package automatically adds the correct prefix (Section, Figure, etc.) and handles pluralization, ranges, and language-specific formatting.
\cite, \citep, \citet) not manual references[1] or (Smith 2020) manually\enquote{...} for quotes, never manual quote marks\enquote{outer \enquote{inner} quote}\begin{displayquote}...\end{displayquote}Anti-pattern: Manual quotes
% INCORRECT
"This is a quote"
``This is a quote''
'single quotes'
Correct: Use csquotes
% CORRECT
\enquote{This is a quote}
\enquote{outer \enquote{inner} quote}
Why: Manual quote marks don't adapt to language settings and can cause typographical inconsistencies. The csquotes package handles all quote styling correctly based on document language.
\emph{...} to emphasize words or phrases\textbf{...} or nested \emph{\emph{...}}Anti-pattern: ALL CAPITALS for emphasis
% INCORRECT
This is VERY important to understand.
We must do this NOW before moving forward.
The BENEFITS of classes are clear.
Correct: Semantic emphasis
% CORRECT
This is \emph{very} important to understand.
We must do this \emph{now} before moving forward.
The \emph{benefits} of classes are clear.
Why: ALL CAPITALS in running text is considered shouting and poor typography. It's harder to read and looks unprofessional. Use \emph{...} to provide semantic emphasis, and LaTeX will render it appropriately (typically as italics, but this can be configured based on context and document style).
Exception: Acronyms and proper names that are conventionally written in capitals (e.g., NASA, USA, PDF) are fine and should not be emphasized.
Core principle: An image is not a figure, but a figure can contain an image. Use proper figure and table environments with captions and labels.
When using the memoir document class, prefer sidecaption over traditional \caption for better layout and accessibility:
For figures:
\begin{figure}
\begin{sidecaption}{Clear description of image content}[fig:label]
\includegraphics[width=0.7\textwidth]{path/to/image}
\end{sidecaption}
\end{figure}
For tables:
\begin{table}
\begin{sidecaption}{Description of table content}[tab:label]
\begin{tabular}{lcc}
\toprule
Header1 & Header2 & Header3 \\
\midrule
Data1 & Data2 & Data3 \\
\bottomrule
\end{tabular}
\end{sidecaption}
\end{table}
Key features of sidecaption:
[fig:label]When to use sidecaption:
Traditional caption alternative:
\begin{figure}
\centering
\includegraphics[width=0.7\textwidth]{path/to/image}
\caption{Description of image content}
\label{fig:label}
\end{figure}
Use traditional \caption + \label when:
Always use \cref{fig:label} (cleveref package) to reference figures and tables:
As shown in \cref{fig:memory-hierarchy}, secondary memory is slower but non-volatile.
The results in \cref{tab:benchmark} demonstrate...
Never hard-code references like "Figure 1", "Table 3.2", or use manual prefixes like Figure~\ref{fig:label}—let cleveref handle both numbering and prefixes automatically.
listings package for code with syntax highlighting\verb for inline code snippetsfigures/diagram.pdf not figures\diagram.pdfWhen writing or editing LaTeX content:
[[code]] notation, not \texttt{...\_...}\textbf{Label:} in itemize, \texttt{..._...} in .nw filesCheck for these common issues:
\textbf{Label:} instead of description environment\ref\S\ref, Section~\ref, Figure~\ref) instead of \cref\cite commands"...", '...', `...`) instead of \enquote{...}figure environmentAdditional checks for .nw literate programming files:
\texttt{..._...} or \texttt{...__...} → Should use [[...]] notation\item[FOO\_BAR behavior] → Use better label + [[FOO_BAR]] in text[[...]] insteadWhen creating Beamer presentations that also generate article versions, use \mode<presentation> and \mode<article> to provide appropriate content for each format.
Verbose text environments: Semantic environments (definition, remark, example, block) with more than 2-3 lines of prose are too verbose for slides.
Solution: Provide concise versions for presentations and full explanations for articles:
\begin{frame}
\mode<presentation>{%
\begin{remark}[Key Point]
\begin{itemize}
\item Concise point 1
\item Concise point 2
\item Concise point 3
\end{itemize}
\end{remark}
}
\mode<article>{%
\begin{remark}[Key Point]
Full explanatory paragraph with detailed reasoning and context
that would overwhelm a slide but provides value in written form.
Additional paragraphs can elaborate on nuances and implications.
\end{remark}
}
\end{frame}
Side-by-side layouts using \textbytext:
\textbytext (non-starred, column width, works in beamer frames)\textbytext* (starred, fullwidth, better for printed documents)\begin{frame}
\mode<presentation>{%
\textbytext{Left content}{Right content}
}
\mode<article>{%
\textbytext*{Left content}{Right content}
}
\end{frame}
Slides need visual clarity and conciseness (bullets, short phrases). Articles can provide depth and explanation (full sentences, paragraphs). Design content appropriate for each medium.
For new LaTeX documents, use the standard preamble from references/preamble.tex. Copy it verbatim to your project's doc/preamble.tex and include it with \input{preamble} after \documentclass.
Document structure:
\documentclass[a4paper,oneside]{memoir}
\input{preamble}
\title{Document Title}
\author{Author Name}
\date{\today}
\begin{document}
\frontmatter
\maketitle
\tableofcontents
\mainmatter
% Content here
\backmatter
\end{document}
The standard preamble provides:
This ensures consistent formatting across all documents and projects.
LaTeX is a document preparation system based on semantic markup, not a word processor. The goal is to describe what content is, not how it should look. Let LaTeX handle the formatting based on the semantic structure you provide.