[LaTeX] subfigures with captions

This post provides Latex code examples for how to generate sub-figures with and without captions.

  • Sub-figures with captions
\documentclass{article}
\usepackage{graphicx, caption, subcaption}
\begin{document}
  This article is about ....

\begin{figure}
 \begin{subfigure}{0.96\textwidth} 
     \includegraphics[width=\textwidth]{subfig1} 
     \caption{subfig 1 caption text here}
 \end{subfigure} 
 \centering  %note: this centering command applies to subfig1
 \hfill
 \begin{subfigure}{0.47\textwidth}
     \includegraphics[width=\textwidth]{subfig2}
     \caption{subfig 2 caption text here}
 \end{subfigure}
 \hfill
 \begin{subfigure}{0.47\textwidth}
     \includegraphics[width=\textwidth]{subfig3}
     \caption{subfig 3 caption text here}
 \end{subfigure}
 \caption{the overall fig caption text here}
 \label{fig:subfig_example} % Give a unique label
\end{figure}

\end{document}
  • Sub-figures without caption
\documentclass{article}
\usepackage{graphicx}
\begin{document}
  This article is about ....

%*For figures without sub-captions
\begin{figure}
    \includegraphics[width=0.96\textwidth]{subfig1}
    \centering 
    \hfill
    \includegraphics[width=.48\textwidth]{subfig2} 
    \hfill
    \includegraphics[width=.48\textwidth]{subfig3}
    % figure caption is below the figure
    \caption{figure caption text here}
 \label{fig:subfig_example2} % Give a unique label
 \end{figure}
 
\end{document}

[LaTeX] Add appendices in an article

This post introduces how to add appendices to an article.

The command \appendix  is included in all basic class files, so you do not need to include any extra package to add appendix, unless the journal that you aim at has specific appendix style requirements.

\begin{document}

\section{Your section name here}
\section{Your section name here}

% Activate the appendix in the doc
% from here on sections are numerated with capital letters 
\appendix

\section{Appendix A title here}
\subsection{Appendix subsection title here}
\subsection{Appendix subsection  title here}

\section{Appendix B title here}

\end{document}

[LaTeX] Optional parameters explained for fine tuning the placement of tables and figures in LaTeX

This post introduces the optional parameters to fine tune the placement of tables and figures in LaTeX.

If you are using LaTeX, you have seen figure examples like the following

\begin{figure}[ht]
...
\end{figure}

These are optional parameters to fine tune the placement of tables and figures, the term is Float placement specifiers (introduced in detail below). LaTeX will try to honor the placement with respect to the actual place, the top or bottom of the page, or a separate page of floats coming immediately after the present insertion point.

Float placement specifiers

To direct a float to be placed into one of these areas, a float placement specifier has to be provided as an optional argument to the float.

If no such optional argument is given then a default placement specifier is used, which depends on the float class. Each float in LaTeX belongs to a class. By default, LaTeX knows about two classes,figureand table. A float placement specifier can contain the following characters in any order and combination:

  • ! — indicates that some restrictions should be ignored 
  • h — indicates that the float is allowed to be placed inline (i.e., here)
  • t — indicates that the float is allowed to go into a top area
  • b — indicates that the float is allowed to go into a bottom area
  • p — indicates the the float is allowed to go on a float page or column area

The order in which these characters are stated does NOT influence how the algorithm tries to place the float (e.g., [ht] or [th] will make no difference)! This is one of the common misunderstandings, for instance when people think that bt means that the bottom area should be tried first. You may also force LaTeX to “insist” on these specifications by adding an exclamation mark (!) before the placement parameters, e.g. \begin{figure}[!htb].

However, if a letter is not present then the corresponding area will not be tried at all.

For more detailed intro about this topic, check out How to influence the position of float environments like figure and table in LaTeX?

 

References:

 

[LaTeX] Use fancyhdr package to control page numbering style

This post introduces how to use fancyhdr package to control page numbering style in your LaTex document.

We can use the LaTeX package fancyhdr to customize how the page numbers are displayed.

For example, if you want to put the current page number in the context of the page numbers in the whole document (page 1 of 10 or 1/10), the following command can help you with that:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}
\usepackage{lastpage}
 
\pagestyle{fancy}
\fancyhf{}


%use the following commands by uncomment (i.e., remove the % for the line you want to use.) 

%uncomment the command below (the part in bold) if you want to show the %style "Page 1 of 10" to the right of your footer)
%\rfoot{Page \thepage \hspace{1pt} of \pageref{LastPage}} 
% change \rfoot to \lfoot or \cfoot for left or center positioning

%uncomment the command below (the part in bold) if you want to show the style "1/10" to the right of your footer)
%\rfoot{\thepage \hspace{1pt}/\pageref{LastPage}} 
% change \rfoot to lfoot or \cfoot for left or center positioning

%uncomment the command below (the part in bold) if you only want to put %the current page number at the center of the header
%\fancyhead[C]{\thepage} 
% change [C] to [R] or [L] for right or left positioning 


 
\begin{document}
 
\tableofcontents
 
\section{First section}
Some text...
 
\section{Second section}
More text...
 
\end{document}

See the references given below for more style needs (e.g., even and odd page numbering).

References:

Page numbering/ Customizing numbering styles

LaTeX/Customizing Page Headers and Footers

 

Use latexdiff to compare different versions of LaTeX tex files to visually mark differences

This post introduces how to use latexdiff to compare two versions of latex *.tex files, in order to visually mark changes to tex files.

(One colleague asked me about the usage of latexdiff, so I think it is a good time to write it up into a blog post to share with those who need some help with this.)

latexdiff is a Perl script and requires an installation of Perl 5.8 or higher.

Use latexdiff to mark differences and create a new LaTeX document with markup differences between two latex files.

  • Linux (Ubuntu) users

latexdiff can be found in Ubuntu repository and detailed description of usage can be found here.

In your terminal, issue the commands below to install latexdiff.

sudo apt-get update
sudo apt-get install latexdiff
# open your terminal, and cd to the tex files you need to compare
# issue the following command
latexdiff original_version.tex revised_version.tex > diff.tex

Run the diff.tex file like you run a normal tex file, you will see beautifully marked differences in the pdf file.

  • Mac OS X users
latexdiff is a Perl script and requires an installation of Perl 5.8 or higher.
Mac OS X includes an installation of Perl and no additional setup is required. latexdiff can be found in any CTAN repository
(If you are using MacTex on your mac,  latexdiff is already installed in MacTex under /usr/texbin/latexdiff. No need to install it separately. To use latexdiff in commend line, see below)
After you setting perl and latexdiff, just open your terminal, and cd to the path where you put you two versions of latex tex files, and then issue the command. (If the two tex files are located in different location, you can just give the relative path to that location for each version of tex file.)

 

$ latexdiff original_version.tex revised_version.tex > diff.tex
will compare original_version.tex to revised_version.tex and create a new file diff.tex showing the differences.
Run the diff.tex file like you run a normal tex file, you will see beautifully marked differences in the pdf file.
  • Online latexdiff 
when you get the diff.tex on the online latexdiff, you can paste into a new tex file and then you can run it and you will see the pdf which shows the differences.
  • Multiple tex files for each version

If each version of your text files contains multiple (sub) tex files or if you want to use latexdiff with Git, check the reference lists for potential solutions.

References:

LATEXDIFF: SUPERB DIFF TOOL FOR LATEX (pdf) — talks about solutions to multiple files, but this can only deal with sub-files on the same level, not nested cases.

Two LaTeX gems: ShareLaTeX and latexdiff (pdf)

Using Latexdiff For Marking Changes To Tex Documents (pdf)

Multiple-file LaTeX diff (pdf— this one is very good. – use python code to merge multiple tex files to one big tex file for each version and then use latexdiff to make comparision.

Latexdiff with subfiles (pdf) — this one talks about solutions to multiple tex files.

Using latexdiff with git (pdf) – Git Latexdiff  — this is about using git together with latexdiff

Git and latexdiff: compare versions of LaTeX documents (pdf) — this is also about using git together with latexdiff

 

 

 

[LaTeX] Comment out some text in a bib file

This post introduces how to comment out some text in a .bib file.

Sometimes we need to keep some information associated with a paper, such as web link where we can find the file, but we do not want it appears in the references.

It said that “//” or “%” works for some cases, but apparently it does not work for me. My solution is use comment = {add the info you wanted to comment with this  pair of curly brackets},

See below for an example.

@article{author2015paper,
title={paper title here},
author={author name},
comment={https://example.pdf},
year={2015}
}

[LaTeX] Write partial differential equation (Ex. dQ/dt=ds/dt) with partial derivative signs

\documentclass{article}
\begin{document}

\begin{equation}
\frac{\partial Q}{\partial t} = \frac{\partial s}{\partial t}
\end{equation}

\end{document}

\begin{equation}
\frac{\partial h}{\partial t} + \frac{\partial uh}{\partial x} + \frac{\partial vh}{\partial y} = 0
\end{equation}

  • equation with numbering
\begin{equation}
 \frac{\partial h}{\partial t} + \frac{\partial uh}{\partial x} + \frac{\partial vh}{\partial y} = 0 
\end{equation}

  • equation with name no numbering
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\newcommand{\eqname}[1]{\tag*{#1}}% Tag equation with name

\begin{document}

\begin{equation}
 \frac{\partial h}{\partial t} + \frac{\partial uh}{\partial x} + \frac{\partial vh}{\partial y} = 0 \eqname{Continuity Equation} \ 
\end{equation}

\end{document}

  • equation with both name and numbering
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\newcommand{\eqname}[1]{\tag*{#1}}% Tag equation with name

\begin{document}

\begin{align}
 \frac{\partial h}{\partial t} + \frac{\partial uh}{\partial x} + \frac{\partial vh}{\partial y} = 0 \\ \eqname{Continuity Equation} \ 
\end{align}

\end{document}

 

  • equation with both name and numbering and ref equation in text body
\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\newcommand{\eqname}[1]{\tag*{#1}}% Tag equation with name
\begin{document}

\begin{align}
 f(x) &= a \\ \eqname{Constant} \ g(x) &= ax \\ \eqname{Linear} \ h(x) &= ax^2+bx+c \label{abc} \\ \eqname{Quadratic}
\end{align}


See~\eqref{abc}.
\end{document}