Latex: Difference between revisions

 
(22 intermediate revisions by the same user not shown)
Line 10: Line 10:
Download [https://miktex.org MikTex].
Download [https://miktex.org MikTex].
It includes the TeXworks editor and will download packages as you use them.
It includes the TeXworks editor and will download packages as you use them.
This will not take up as much disk space as a full install.
This will not take up as much disk space as a full install but will require internet access to download new packages.
===Full Install===
===Full Install===
====Windows====
====Windows====
Download [https://www.tug.org/texlive/ TexLive].
Download [https://www.tug.org/texlive/ TexLive] using the [https://www.tug.org/texlive/acquire-iso.html ISO images].
This is several gigabytes since it includes all the popular LaTex packages and takes a while to install.
This is ~4 gigabytes since it includes all the popular LaTeX packages and takes a while to install.
You'll also need an editor. I recommend installing [[Atom_(text_editor) | Atom]] with the following packages:  
You'll also need an editor. I recommend installing [[Atom_(text_editor) | Atom]] with the following packages:  
* <code>latex</code> for calling the TexLive compiler
* <code>latex</code> for calling the TexLive compiler
* <code>language-latex</code> for syntax highlighting
* <code>language-latex</code> for syntax highlighting
* <code>pdf-view</code> for viewing the compiled pdf.
* <code>pdf-view</code> for viewing the compiled pdf.
<pre>
apm install latex language-latex pdf-view
</pre>


====Linux====
====Linux====
Line 28: Line 31:
pdflatex [mydocument.tex]
pdflatex [mydocument.tex]
</syntaxhighlight>
</syntaxhighlight>
* Note to add bibliography, you need to run <code>pdflatex</code>, <code>bibtex</code>, <code>pdflatex</code> or <code>rubber --pdf</code>.
==Usage==
==Usage==


Line 77: Line 83:
\end{minipage}
\end{minipage}
</pre>
</pre>


===Sections===
===Sections===
Line 96: Line 101:
===Custom Commands===
===Custom Commands===
You can define your own commands using <code>\newcommand</code>
You can define your own commands using <code>\newcommand</code>
<syntaxhighlight lang="latex">
\newcommand{\notdone}{{\color{red} Not done!!}}
</syntaxhighlight>


===Custom Operators===
===Custom Operators===
Line 151: Line 159:
For multirow cells, you can use <code>\multirowcell{5}{Numbers\\from\\ 1 to 5}</code>.   
For multirow cells, you can use <code>\multirowcell{5}{Numbers\\from\\ 1 to 5}</code>.   
By default, this will align center. You can make it align left: <code>\multirowcell{2}[0pt][l]{Number\\ Letter}</code>.
By default, this will align center. You can make it align left: <code>\multirowcell{2}[0pt][l]{Number\\ Letter}</code>.
==Table Generator==
[https://www.tablesgenerator.com/ https://www.tablesgenerator.com/] can generate table code from existing tables.
==Figures==
<syntaxhighlight lang="latex">
\begin{figure}[!htbp]
  \includegraphics[width=\linewidth]{/path/to/figure}
  \caption{}
  \label{}
\end{figure}
</syntaxhighlight>
Be sure to use vector figures (pdf or pgf) instead of raster ones (png, jpeg).<br>
See [https://timodenk.com/blog/exporting-matplotlib-plots-to-latex/ https://timodenk.com/blog/exporting-matplotlib-plots-to-latex/] on how to export matplotlib to pgf.
In the snippet <code>[!htbp]</code> represents the placement preferences.
* <code>!</code> means to ignore some placement limitations
* <code>h</code> mean to place the figure here
* <code>t</code> means top of the page.
* <code>b</code> means bottom of the page.
* <code>p</code> means to place it on a figure only page.
If you add the [https://ctan.mirrors.hoobly.com/macros/latex/contrib/float/float.pdf the float package], you can also use <code>[!H]</code> which is a more strict version of <code>[!h]</code>.
===Subfigure===
See [https://www.overleaf.com/learn/latex/How_to_Write_a_Thesis_in_LaTeX_(Part_3):_Figures,_Subfigures_and_Tables#Subfigures overleaf reference]
<syntaxhighlight lang="latex">
% In the header:
\usepackage{caption}
\usepackage{subcaption}
\begin{figure}[!htbp]
  \centering
  \begin{subfigure}[b]{0.45\linewidth}
    \centering
    \includegraphics[width=\linewidth]{graph1}
    \caption{This is graph1.}
    \label{fig:a}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{0.45\linewidth}
    \centering
    \includegraphics[width=\linewidth]{graph2}
    \caption{This is graph2.}
    \label{fig:b}
  \end{subfigure}
  \caption{}
  \label{fig:ab}
\end{figure}
</syntaxhighlight>


==Enumerate==
==Enumerate==
Line 184: Line 244:
<syntaxhighlight lang="latex">
<syntaxhighlight lang="latex">
\newcommand{\degree}{\ensuremath{^{\circ}} }
\newcommand{\degree}{\ensuremath{^{\circ}} }
\newcommand{\etal}{{\em et al. }}
\newcommand{\etal}{{\em et al.}}
\newcommand{\ceil}[1]{{\lceil #1 \rceil}}
\newcommand{\ceil}[1]{{\lceil #1 \rceil}}
\newcommand{\floor}[1]{{\lfloor #1 \rfloor}}
\newcommand{\floor}[1]{{\lfloor #1 \rfloor}}
\newcommand{\bmat}[1]{\begin{bmatrix}#1\end{bmatrix}}
\newcommand{\pmat}[1]{\begin{pmatrix}#1\end{pmatrix}}
</syntaxhighlight>
</syntaxhighlight>


Line 264: Line 327:
\usepackage[linesnumbered,ruled]{algorithm2e}
\usepackage[linesnumbered,ruled]{algorithm2e}


\begin{algorithm}[H]
\begin{algorithm}[!htbp]
  \KwData{this text}
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }
  \KwResult{how to write algorithm with \LaTeX2e }
Line 280: Line 343:
\end{algorithm}
\end{algorithm}
</syntaxhighlight>
</syntaxhighlight>
==Verbatim==
The <code>verbatim</code> package gives you the environment <code>verbatim</code>.
You can use <code>\verbatiminput</code> to embed text files.
===<code>fancyvrb</code>===
The <code>fancyvrb</code> package gives you the environment <code>Verbatim</code> which has more options. 
To embed in a figure, you can use <code>BVerbatim</code>. 
There is also <code>\VerbatimInput</code>, <code>\BVerbatimInput</code>, <code>\LVerbatimInput</code>.


==Document Class==
==Document Class==
Line 337: Line 409:
</pre>
</pre>
* <code>-r</code> specifies the DPI. Default is 150.
* <code>-r</code> specifies the DPI. Default is 150.
===Latex Cleanup===
If you need to submit the latex, e.g. to Arxiv, you can use this tool to help clean it up:
https://github.com/google-research/arxiv-latex-cleaner


==Resources==
==Resources==
* [http://detexify.kirelabs.org/classify.html Detexify LaTeX handwritten symbol recognition]
* [http://detexify.kirelabs.org/classify.html Detexify LaTeX handwritten symbol recognition]
[[Category:Programming languages]]