Sphinx hangs when attempting to use amxmath capabilities - latex

I want to define a matrix in a math block in a ReST file like this;
.. math::
\[\begin{bmatrix}a_1 & b_1\\ a_2 & b_2\end{bmatrix}\]
In my conf.py I defined a preamble in order to include the LaTeX package amsmath
latex_elements = {
# Additional stuff for the LaTeX preamble.
'preamble': r'''
\usepackage{amsmath}
'''
}
However when I try and compile my document using Sphinx, it gets to the document and hangs there permanently
writing output... [ 97%] theory_and_methodology
What am I missing?

I found the answer. The LaTeX compiler didn't like the \[ in the source. It was unnecessary, so when I removed it the output worked.

Related

How to implement full LaTeX syntax in Rmarkdown?

I have a two part question regarding the use of LaTeX in Rmarkdown:
1) When working in Rmarkdown (with the intent to render to PDF), is there a rule for when we should just use the double dollar signs ($$) to write something in LaTeX or when we should use the LaTeX syntax to begin and end all of our LaTeX code (e.g. \documentclass{...}, \begin{document}, \end{document}, etc.
I believe I have read that it is okay to just use the latter option, and Rmarkdown will ignore all of the escaped latex commands if the document is rendered to anything other than PDF.
2) The reason I am asking, in this case, is that I am trying to incorporate some labelled matrix multiplication in an Rmarkdown document I am writing. Specifically, I would like to include some matrices that take the form show on this page. Here is the code:
\documentclass{article}
\usepackage{amsmath}
\newenvironment{spmatrix}[1]
{\def\mysubscript{#1}\mathop\bgroup\begin{pmatrix}}
{\end{pmatrix}\egroup_{\textstyle\mathstrut\mysubscript}}
\begin{document}
\begin{equation}
\begin{spmatrix}{A}
a & b \\
c & d
\end{spmatrix}
\begin{spmatrix}{x}
x_1 \\
x_2
\end{spmatrix}
=
\begin{spmatrix}{b}
b_1 \\
b_2
\end{spmatrix}
\end{equation}
\end{document}
How would one implement this code in Rmarkdown? Do you need to move the \usepackage call into the YAML as suggested in other threads discussing the loading of LaTeX packages in Rmarkdown? Is the first line, \documentclass{article} even needed within an Rmarkdown document?
I'm new to all of this, and thusfar, I've been able to get by using the double dollar signs to set off all my LaTeX code for simple equations and even a simple matrix here and there that I've tried to write, but for mathematical expressions that require more formatting, aligning, multi-line proofs, etc., most of the examples I've encountered are on the TEX boards written with syntax similar to the code above. I haven't been able to figure out how to implement these types of examples in Rmarkdown. Any helpful suggestions or pointers where to better understand this issue would be much appreciated!
It is indeed possible to put most body-level LaTeX constructs into the body of your Rmd file. Other backends will ignore these constructs, but the result might look change. So from my point of view your are binding yourself to PDF output. But that might be fine in your case.
Concerning your concrete problem:
amsmath is already included by the default template, which also takes care of \ḑocumentclass and the document environment.
You need to add the environment definition into a separate tex file (in my case preamble.tex) and include that file via the YAML headers.
You can then use the LaTeX constructs as is in the Rmd body.
Putting things together:
---
output:
pdf_document:
keep_tex: yes
includes:
in_header: preamble.tex
---
\begin{equation}
\begin{spmatrix}{A}
a & b \\
c & d
\end{spmatrix}
\begin{spmatrix}{x}
x_1 \\
x_2
\end{spmatrix}
=
\begin{spmatrix}{b}
b_1 \\
b_2
\end{spmatrix}
\end{equation}

How can I mention the name of a LaTeX command in my beamer presentation?

I am preparing a presentation in beamer with gradientframe package. But it's giving me an error.
How can I write the name of the command in \frame?
\begin{frame}
\frametitle{Introduction}
The gradientframe package provides a command, \gradientframe for simple and discreet
rectangular grayscale gradient frames around objects, such as figures or tables, to set
them apart from the surrounding text.
\end{frame}
As in several answers to the linked question (thanks #Bas Swinckels), the way advised to insert a backslash \ in the output is to use \textbackslash in the source.
Then, you can use (or not) just \texttt{} (rather than verbatim or else) to mark command names.
Finally, if you have to mention these command name(s) many times in your slides, you may want to put the above two in a newcommand:
\documentclass{beamer}
\newcommand{\mycomm}[1]{\texttt{\textbackslash #1}}
%\newcommand{\commgf}{\texttt{\textbackslash gradientframe}}
\begin{document}
\begin{frame}
\frametitle{Introduction}
... a command, \textbackslash gradientframe for simple ...
or:
... a command, \texttt{\textbackslash gradientframe} for simple ...
or, better:
... a command, \mycomm{gradientframe} for simple ...
%... a command, \commgf{} for simple ...
\end{frame}
\end{document}
This code gives you:

Inserting code in this LaTeX document with indentation

How do I insert code into a LaTeX document? Is there something like:
\begin{code}## Heading ##
...
\end{code}
The only thing that I really need is indentation and a fixed width font. Syntax highlighting could be nice although it is definitely not required.
Use listings package.
Simple configuration for LaTeX header (before \begin{document}):
\usepackage{listings}
\usepackage{color}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstset{frame=tb,
language=Java,
aboveskip=3mm,
belowskip=3mm,
showstringspaces=false,
columns=flexible,
basicstyle={\small\ttfamily},
numbers=none,
numberstyle=\tiny\color{gray},
keywordstyle=\color{blue},
commentstyle=\color{dkgreen},
stringstyle=\color{mauve},
breaklines=true,
breakatwhitespace=true,
tabsize=3
}
You can change default language in the middle of document with \lstset{language=Java}.
Example of usage in the document:
\begin{lstlisting}
// Hello.java
import javax.swing.JApplet;
import java.awt.Graphics;
public class Hello extends JApplet {
public void paintComponent(Graphics g) {
g.drawString("Hello, world!", 65, 95);
}
}
\end{lstlisting}
Here's the result:
You could also use the verbatim environment
\begin{verbatim}
your
code
example
\end{verbatim}
Here is how to add inline code:
You can add inline code with {\tt code } or \texttt{ code }. If you want to format the inline code, then it would be best to make your own command
\newcommand{\code}[1]{\texttt{#1}}
Also, note that code blocks can be loaded from other files with
\lstinputlisting[breaklines]{source.c}
breaklines isn't required, but I find it useful. Be aware that you'll have to specify \usepackage{ listings } for this one.
Update: The listings package also includes the \lstinline command, which has the same syntax highlighting features as the \lstlisting and \lstinputlisting commands (see Cloudanger's answer for configuration details). As mentioned in a few other answers, there's also the minted package, which provides the \mintinline command. Like \lstinline, \mintinline provides the same syntax highlighting as a regular minted code block:
\documentclass{article}
\usepackage{minted}
\begin{document}
This is a sentence with \mintinline{python}{def inlineCode(a="ipsum)}
\end{document}
Specialized packages such as minted, which relies on Pygments to do the formatting, offer various advantages over the listings package. To quote from the minted manual,
Pygments provides far superior syntax highlighting compared to conventional packages. For example, listings basically only highlights strings, comments and keywords. Pygments, on the other hand, can be completely customized to highlight any token kind the source language might support. This might include special formatting sequences inside strings, numbers, different kinds of identifiers and exotic constructs such as HTML tags.
Minted, whether from GitHub or CTAN, the Comprehensive TeX Archive Network, works in Overleaf, TeX Live and MiKTeX.
It requires the installation of the Python package Pygments; this is explained in the documentation in either source above. Although Pygments brands itself as a Python syntax highlighter, Minted guarantees the coverage of hundreds of other languages.
Example:
\documentclass{article}
\usepackage{minted}
\begin{document}
\begin{minted}[mathescape, linenos]{python}
# Note: $\pi=\lim_{n\to\infty}\frac{P_n}{d}$
title = "Hello World"
sum = 0
for i in range(10):
sum += i
\end{minted}
\end{document}
Output:
Use Minted.
It's a package that facilitates expressive syntax highlighting in LaTeX using the powerful Pygments library. The package also provides options to customize the highlighted source code output using fancyvrb.
It's much more evolved and customizable than any other package!
A very simple way if your code is in Python, where I didn't have to install a Python package, is the following:
\documentclass[11pt]{article}
\usepackage{pythonhighlight}
\begin{document}
The following is some Python code
\begin{python}
# A comment
x = [5, 7, 10]
y = 0
for num in x:
y += num
print(y)
\end{python}
\end{document}
which looks like:
Unfortunately, this only works for Python.
Since it wasn't yet mentioned here, it may be worth to add one more option, package spverbatim (no syntax highlighting):
\documentclass{article}
\usepackage{spverbatim}
\begin{document}
\begin{spverbatim}
Your code here
\end{spverbatim}
\end{document}
Also, if syntax highlighting is not required, package alltt:
\documentclass{article}
\usepackage{alltt}
\begin{document}
\begin{alltt}
Your code here
\end{alltt}
\end{document}
Use Pygments !

LaTeX listings package: copy-pastable listings

Writing some docs with code snippets which I want to be copyable to run as written. These snippets may include lines with preceding spaces. The listings package formats the text fine, but the spaces are not copyable.
Let's say I have the following example:
\documentclass{article}
\usepackage{listings}
\begin{document}
\lstset{
basicstyle=\ttfamily,
frame=single,
columns=fullflexible
}
\begin{lstlisting}[language=python]
def foo():
return "bar"
\end{lstlisting}
\end{document}
If I copy and paste the listing somewhere, it becomes:
def foo():
return "bar"
which must be corrected by hand.
Is there a way to make the listings package include the original spaces? Or is there a package better suited for cases like this?
This is (most likely) not a problem with listings (or latex at all), but with your PDF rendering software. For instance, with PDFKit-based (Preview, Skim, ...) on OSX, I get the behavior that you describe. By using Xpdf, however, the text is copied correctly.

How to show LaTeX-code in a LaTeX document?

I have a little problem where I would like to insert a svn diff of a LaTeX document into
another LaTeX document, the goal is to show what has changed since revision XXX.
However since the diff contains a lot of LaTeX command I can't include it right into the document since LaTeX will interpit them and not just "print" them.
Today I have this in my Makefile
DIFF_INFO=diff.info.tex
DIFF_REV=167
diffinfo:
$(shell echo "\n" > $(DIFF_INFO) )
$(shell echo "\\section{diff $(DIFF_REV)} \n" >> $(DIFF_INFO) )
$(shell echo \\\\begin{verbatim} >> $(DIFF_INFO) )
$(shell svn diff --revision $(DIFF_REV) $(N).tex >> $(DIFF_INFO) )
$(shell echo \\\\end{verbatim} >> $(DIFF_INFO) )
And at the end of the LaTeX document I have this:
\IfFileExists{diff.info.tex}
{
\newpage
\input{diff.info.tex}
}
But this fails hard!
My next idea is to write a perl script that replaces all invalid chars with something that LaTeX can show, but it feels like I'm risking to reinvent the wheel so I figured that I could ask if someone had a better idea?
How do I include and show LaTeX code in the document?
Thanks
Johan
Update:
Thanks "unknown (google)" for pointing out verbatim, it did what I wanted it to.
Update:
I also looks like I should try that listings that las3rjock told us about since it look kind of nice.
Update:
Could not get listings to work in my case, I get some strange utf warnings about invalid chars. But the verbatim is working so I will use that way this time.
I second Boojum's recommendation in a comment to another answer that you use the listings package. For LaTeX code listings, I use settings that I found in André Miede's classicthesis package. Here is a sample document (quine.tex) and its output:
\documentclass[12pt,letterpaper]{article}
\usepackage{listings}
\usepackage[usenames,dvipsnames]{color}
% listings settings from classicthesis package by
% Andr\'{e} Miede
\lstset{language=[LaTeX]Tex,%C++,
keywordstyle=\color{RoyalBlue},%\bfseries,
basicstyle=\small\ttfamily,
%identifierstyle=\color{NavyBlue},
commentstyle=\color{Green}\ttfamily,
stringstyle=\rmfamily,
numbers=none,%left,%
numberstyle=\scriptsize,%\tiny
stepnumber=5,
numbersep=8pt,
showstringspaces=false,
breaklines=true,
frameround=ftff,
frame=single
%frame=L
}
\begin{document}
\lstinputlisting{quine.tex}
\end{document}
(click to enlarge)
There' a verbatim package that you can include with \usepackage{verbatim} and access using \verbatiminput{filename}.

Resources