Skip to content

Instantly share code, notes, and snippets.

@ajmalanoski
Created February 18, 2023 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajmalanoski/c2b77a20e59b16459da9e6717217c860 to your computer and use it in GitHub Desktop.
Save ajmalanoski/c2b77a20e59b16459da9e6717217c860 to your computer and use it in GitHub Desktop.
XeLaTeX source for my how-to on typesetting semantics in LaTeX
% XeLaTeX can use any Mac OS X font. See the setromanfont command below.
% Input to XeLaTeX is full Unicode, so Unicode characters can be typed directly into the source.
% The next lines tell TeXShop to typeset with xelatex, and to open and save the source with Unicode encoding.
%!TEX TS-program = xelatex
%!TEX encoding = UTF-8 Unicode
\documentclass[12pt]{article}
\usepackage{amssymb}
\usepackage{fontspec,xltxtra,xunicode}
\setmainfont[Mapping=tex-text]{CMU Serif}
\setmonofont[Scale=MatchLowercase]{Andale Mono}
\usepackage{geometry}
\geometry{letterpaper}
\usepackage[linguistics]{forest}
\usepackage{qtree}
\usepackage{stmaryrd}
\usepackage{mathtools}
\DeclarePairedDelimiter\set\{\}
\DeclarePairedDelimiter\denote\llbracket\rrbracket
\DeclarePairedDelimiter\type\langle\rangle
\usepackage{gb4e}
\title{Typesetting semantics in \LaTeX}
\author{Aidan Malanoski}
%\date
\begin{document}
\maketitle
The goal of this document is to provide readers with basic skills for typesetting semantics in \LaTeX. I have tried not to assume too much \LaTeX\ knowledge from readers, so it should be accessible even to people starting out in \LaTeX. Feel free to copy-paste commands and such from the \texttt{.tex} file.
For a lot of semantics typesetting, you'll be in math mode. When you want your math-y stuff (including logic) to be on the same line as your text (\textit{i.e.}, you want an ``in-line'' equation), you enter math mode by putting a single dollar sign \$ on either side of the math-y stuff. For example, the code \texttt{\$2 \textbackslash times 2 = 4\$} gives $2 \times 2 = 4$. If you want your math-y stuff to be on its own line, then you use two dollar signs instead of one. For example, the code \texttt{\$\$2 \textbackslash times 2 = 4\$\$} gives the following: $$2 \times 2 = 4$$
If your math-y stuff needs to be on multiple lines, then you can use the \texttt{\textbackslash align} environment from the \texttt{mathtools} package (the \texttt{amsmath} package also provides an \texttt{\textbackslash align} environment, but we'll use \texttt{mathtools} since it provides other useful functionality). To load the package, you put \texttt{\textbackslash usepackage\{mathtools\}} in the ``preamble,'' which is the part of the \texttt{.tex} file before the \texttt{\textbackslash begin\{document\}} command. Generally speaking, you load other \LaTeX\ packages the same way.
To create an \texttt{align} environment, you first type \texttt{\textbackslash begin\{align\}}. As the name suggests, the \texttt{align} environment lets you align the lines of your math-y stuff. To do this, you oput an ampersand (\&) before the character where you want the lines to be aligned. You separate lines using two backslashes (\texttt{\textbackslash\textbackslash}). This is \LaTeX's general ``new line'' command---it's not specific to the \texttt{align} environment). When you're done writing your math-y stuff, you put \texttt{\textbackslash end\{align\}}. Here's an example of some multi-line math-y stuff made in an \texttt{align} environment.
\begin{align}
2x + 4 &= 14 \\
2x &= 10 \\
x &= 5
\end{align}
To make this, I used the code in (\ref{ex:align}). Note that by putting ampersands before the equals signs in the code, I aligned the equations on the equals signs.
\begin{exe}
\ex\label{ex:align}
\tt
\textbackslash begin\{align\} \\
2x + 4 \&= 14 \textbackslash\textbackslash \\
2x \&= 10 \textbackslash\textbackslash \\
x \&= 5 \\
\textbackslash end\{align\}
\end{exe}
If you don't want the ``tags'' (\textit{i.e.}, the line numbers) on the right side of the page, then you can use the \texttt{align*} environment instead. Just replace \texttt{\textbackslash begin\{align\}} with \texttt{\textbackslash begin\{align*\}} and replace \texttt{\textbackslash end\{align\}} with \texttt{\textbackslash end\{align*\}}. You can add or change the tags using the \texttt{tag} command. Type this command before the new line command \texttt{\textbackslash\textbackslash} on the line where you want it to go. For example, if I change the first line of (\ref{ex:align}) to \texttt{2x + 4 \&= 14 \textbackslash tag\{bloop\} \textbackslash\textbackslash}, I get the following:
\begin{align}
2x + 4 &= 14 \tag{bloop} \\
2x &= 10 \\
x &= 5
\end{align}
As alluded to above, you need to be in math mode to type logic symbols. The command \texttt{\textbackslash land} will give you the logical conjunction (`and') symbol $\land$, \texttt{\textbackslash lor} will give you the logical disjunction (`or') symbol $\lor$, \texttt{\textbackslash rightarrow} will give you the material implication (`if then') symbol $\rightarrow$, and \texttt{\textbackslash leftrightarrow} will you give you the the biconditional (`if and only if') symbol $\leftrightarrow$.
You'll probably be using set theory, too. Once again, you'll need to be in math mode. The \texttt{\textbackslash cup} command produces the union symbol $\cup$, \texttt{\textbackslash cap} produces the intersection symbol $\cap$, \texttt{\textbackslash subseteq} produces the subset symbol $\subseteq$, \texttt{\textbackslash subset} produces the proper subset symbol $\subset$, and \texttt{\textbackslash emptyset} produces the empty set symbol $\emptyset$. However, you can get a prettier empty set symbol $\varnothing$ by using the \texttt{\textbackslash varnothing} command from the \texttt{amssymb} package.
Curly brackets are produced with the commands \texttt{\textbackslash \{} and \texttt{\textbackslash \}}. Typing the curly brackets directly, without the backslash, will not work: at best, they won't show up, and at worst, you'll get an error. This is because curly brackets are used by the \LaTeX\ code to group things together and specify the scope of commands.
Angle brackets are produced with the commands \texttt{\textbackslash langle} and \texttt{\textbackslash rangle} for the left angle bracket $\langle$ and right angle bracket $\rangle$, respectively. To produce double brackets, you need to load the package \texttt{stmaryrd}. The commands are \texttt{\textbackslash llbracket} and \texttt{\textbackslash rrbracket} for the left bracket $\llbracket$ and right bracket $\rrbracket$, respectively. The commands to produce angle brackets and double brackets only work in math mode.
Typing all these brackets gets old pretty fast. Fortunately, the \texttt{\textbackslash mathtools} has a command \texttt{\textbackslash DeclarePairedDelimiter} that can make our life easier. It'll be easier to explain how this command works after seeing an example. The preamble of this document contains the command in (\ref{ex:delimiter}). This creates a new command \texttt{\textbackslash denote} that places double brackets around its input. For example, \texttt{\$\textbackslash denote\{dog\}\$} gives $\denote{dog}$. Note that the command goes in math mode. As you can see, plain text entered in math mode will appear in italics; if you don't want that, use the \texttt{\textbackslash textrm} command. If I change the previous example to \texttt{\$\textbackslash denote\{\textbackslash textrm\{dog\}\}\$}, it will produce $\denote{\textrm{dog}}$.
\begin{exe}
\ex\label{ex:delimiter} \texttt{\textbackslash DeclarePairedDelimiter\textbackslash denote\textbackslash llbracket\textbackslash rrbracket}
\end{exe}
Now, let's take another look at (\ref{ex:delimiter}). As we just saw, \texttt{\textbackslash DeclarePairedDelimiter} is a command that makes new commands (specifically, commands to make ``delimiters''). To use this command, you first type \texttt{\textbackslash DeclarePairedDelimiter}. Then you type the name you want for the new command---in this case, \texttt{\textbackslash denote}. Next, you put the command to make the thing you want as the left delimiter. Here, we want a left double bracket $\llbracket$, so we put \texttt{\textbackslash llbracket}. Finally, you put the command to make the thing you want as the right delimiter. This is a right double bracket $\rrbracket$, so we put \texttt{\textbackslash rrbracket}. In the preamble to this document, I've also defined a command \texttt{\textbackslash set} for putting things in curly brackets and a command \texttt{\textbackslash type} for putting things in angle brackets.
\sloppy
Finally, you may occasionally find yourself typesetting trees while doing semantics. While there a number of packages for creating trees, the \texttt{forest}\footnote{http://tug.ctan.org/info/forest-quickstart/forest-quickstart.pdf} package seems to be increasingly popular. To load this package, put \texttt{\textbackslash usepackage[linguistics]\{forest\}} in the preamble. In \LaTeX, square brackets are used for optional inputs to a command. In this case, we are telling the \texttt{forest} package to load its settings for producing linguistic trees. To produce trees, you create a \texttt{forest} environment, and then write the tree structure using bracket notation. To produce triangles, you put a comma after the node name, then put \texttt{roof}. For example, (\ref{ex:tree-code}) produces (\ref{ex:tree}).
\begin{exe}
\ex
\begin{xlist}
\ex\label{ex:tree-code} \texttt{[TP [DP [D [the]] [NP [sleepy dog, roof]]] [T' [T ] [VP [fell asleep, roof]]]]}
\ex\label{ex:tree}
\begin{forest}
[TP [DP [D [the]] [NP [sleepy dog, roof]]] [T' [T ] [VP [fell asleep, roof]]]]
\end{forest}
\end{xlist}
\end{exe}
If you want to annotate a node with its semantic type, then you can put the node in curly brackets, put a new line \texttt{\textbackslash\textbackslash} after the node label, and put the semantic type. For example, by putting \texttt{\{TP\textbackslash\textbackslash\$t\$\}} instead of just \texttt{TP}, I can add a semantic type annotation below the node name. The type-annotated version of (\ref{ex:tree}) is shown in (\ref{ex:ann-tree}), and the code that produces it is shown in (\ref{ex:ann-tree-code}).
\begin{exe}
\ex
\begin{xlist}
\ex\label{ex:ann-tree-code} \texttt{[\{TP\textbackslash\textbackslash\$t\$\} [\{DP\textbackslash\textbackslash\$e\$\} [\{D\textbackslash\textbackslash\$\textbackslash type\{\textbackslash type\{e,t\}, e\}\$\} [\{the\textbackslash\textbackslash\$\textbackslash type\{\textbackslash type\{e,t\}, e\}\$\}]] [\{NP\textbackslash\textbackslash\$\textbackslash type\{e,t\}\$\} [sleepy dog, roof]]] [\{T'\textbackslash\textbackslash\$\textbackslash type\{e,t\}\$\} [\{T\textbackslash\textbackslash\$\textbackslash varnothing\$\} ] [\{VP\textbackslash\textbackslash\$\textbackslash type\{e,t\}\$\} [fell asleep, roof]]]]}
\ex\label{ex:ann-tree}
\begin{forest}
[{TP\\$t$} [{DP\\$e$} [{D\\$\type{\type{e,t}, e}$} [{the\\$\type{\type{e,t}, e}$}]]
[{NP\\$\type{e,t}$} [sleepy dog, roof]]]
[{T'\\$\type{e,t}$} [{T\\$\varnothing$}]
[{VP\\$\type{e,t}$} [fell asleep, roof]]]]
\end{forest}
\end{xlist}
\end{exe}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment