Showing posts with label latex. Show all posts
Showing posts with label latex. Show all posts

Friday, September 8, 2023

Merging BibTeX bibliography files

Suppose you want to merge two bib files (f1.bib and f2.bib) that have considerable overlap. One easy solution using Jabref works as described below.

Suppose the target bibliography file without duplicates is merge.bib.

1. Copy f1.bib to merge.bib [cp f1.bib merge.bib]

2. Open merge.bib with Jabref

3. Then click File > Import into current database and select the other file [f2.bib]

4. You get a dialog box which allows you to manually decide what entries/versions you want to retain. If both f1.bib and f2.bib are of comparable quality, you can select "Deselect all duplicates" which automatically unselects duplicated entries.

5. Hit "OK" and save the modfied database [Ctrl-S]

Tuesday, November 8, 2022

LaTeX to Word

Often I have a document in LaTeX, and somebody else needs an editable copy in Word. Here is a list of hacks I have learnt to use:

1. If the document is relatively free of math and figures then the simplest course is often to compile a PDF, and "import" the PDF into MS Word. This works out remarkably well in many cases.

2. The same thing above applies to figures. You can now directly drop PDF images into a Word doc.

3. If you have lots of equations, then it is worthwhile to use pandoc

pandoc mydoc.tex -o mydoc.docx

More sophisticated options to copy cross-references, and bibliography exist. See this as well.

4. Many journals accept PDF figures. If they need TIFF, then you can use Adobe Acrobat online to do this conversion. In my experience, this produces smaller files compared to other automatic converters including ImageMagick.

Friday, October 18, 2019

LaTeX: Cross-referencing between Different Documents

Problem: I have a manuscript TeX file (main.tex), and an independent supporting information file (si.tex). I was to cross-reference (using \label and \ref) items across the two files.

For example, I might want to reference figure 1 from si.tex in main.tex.

Solution: As this SO answer suggests, the answer lies in the CTAN package xr.

In main.tex, just include "si.tex" as an external documents, and all its labels become visible!

\usepackage{xr}
\externaldocument{si}

Tuesday, June 12, 2018

My LaTeX Cheat Sheet

Here is a link to my LaTeX cheatsheet.

Here is a list of some useful LaTeX related links from the past:

Monday, November 13, 2017

Exporting Numpy Arrays and Matrices to LaTeX

Over the past couple of years, a lot of my "numerical experimentation" work has moved from Octave to python/numpy.

I incorporate a lot of this work into my classes and presentations (made using beamer), and having a script to translate vectors and matrices to LaTeX format is handy.

In the past, I shared a Matlab/Octave script which does this.

Here is a python/numpy script which does something similar. The script

  • autodetects integers and floats
  • allows you to control the number of decimals for floats
  • allows you optionally render floats in scientific format
  • right-justify using the bmatrix* environment (good for -ve numbers)
  • suppress small values near zero (~ 1e-16)

Wednesday, October 4, 2017

LaTeX: Figure Captions

A minimal working example.




Monday, February 8, 2016

Controlling Footnote Timing in Beamer Presentations

If you primarily use the \pause command to control the when objects appear in a Beamer presentation, you may have encountered some problems where footnotes appear before they are supposed.

A minimal working example:

\begin{itemize}
 \item Mary had a little lamb
 \pause
 \item Humpty Dumpty sat on a wall\footnote{be careful when you sit on walls}
 \pause
 \item ABCDEFG
\end{itemize}


The footnote associated with bullet-point 2 did not wait for the second bullet point to appear. It was present from the very first slide. The \only tag provides an easy way out.

\begin{itemize}
 \item Mary had a little lamb
 \pause
 \item Humpty Dumpty sat on a wall\only<2->\footnote{be careful when   you sit on walls}
 \pause
 \item ABCDEFG
\end{itemize}


Friday, April 4, 2014

Displaying Highlighted Code in LaTeX/Beamer

It is possible to produce "automatically" highlighted code in LaTeX/Beamer using the "lstlisting" package. It supports a wide variety of languages, and one can control color-schemes, treatment of columns, tabs etc.

To produce the following Octave code,


I used the following LaTeX code:



Friday, December 13, 2013

Transition Slides in Beamer

Imagine that you want to produce a series of three transition or thematically linked slides. By that, I mean that most of the information on the slide remains the same, with minor additions, deletions or changes.

As an example consider the following three frames:




The first step, in this case, would be to draw the three figures in a drawing program (like Inkscape), preferably one which supports layers.

I like to draw a border around the image. That way, when I export the image into PNG or PDF format the dimensions of the image are conserved.

Next, we use the following self-explanatory TeX code:

Tuesday, March 19, 2013

Beamer, pause, and space after equations

"\pause" is a very commonly used command in Beamer presentations to prevent displaying all of the slide at once.

In general, it works quite well as this minimal example shows:


Here the pause command was used between the equation and the line "This is the eigenvalue problem".

No problem, right (no pun intended)!

Using an itemize environment can cause some funny extra-space to be inserted between an equation and the next line:


Notice the extra space between the equation and the next item.

A relatively easy fix is to insert a blank line between the equation and the next item.


The code used to generate the three cases is below.



Monday, February 11, 2013

Beamer Howto: Scale Equations to Fit a Slide

A quick and dirty way to scale equations, especially equations with matrices spelled out, so that they fit on a line uses the commonly used graphicx package.

In the preamble say:

\newcommand\scalemath[2]{\scalebox{#1}{\mbox{\ensuremath{\displaystyle #2}}}}

And when you need to use it say something like $\scalemath{0.7}{rest of the equation}$

Wednesday, January 30, 2013

"Handwrite" LaTeX Equations

I've been a big fan and user of deTeXify, which recognizes handwritten symbols and spits out the appropriate LaTeX tags.

Now, this site takes it one step further (via Ramanan on Facebook). You can write entire equations by hand; the program produces LaTeX (and MathML if needed) markup for the whole enchilada.

Thursday, January 24, 2013

A TikZ video

TikZ is among the newer toys (new as in "new to me") I have been playing with since the summer of last year.

Here is a video which demonstrates the use of some nice TikZ libraries


Friday, January 11, 2013

writeLaTeX: Compile LaTeX in a Browser and Collaborate

Just found out about writeLaTeX (via TeXBlog), which lets you write LaTeX "code" and compile it without requiring any software download. The best part is that it is collaborative, which means that multiple people can work on the same document seamlessly.

From the About Page:
  • Effortless Sharing – Every document you create has a secret link. Just send it to your co-authors, and they can review, comment and edit.
  • Create Beautiful Documents Together – writeLaTeX synchronizes changes from all authors transparently, so everyone always has the latest version.
  • Not Just Papers – Make amazing presentations with the beamer package! The preview helps with complicated tables, figures and graphs.
  • Real-time Preview – writeLaTeX compiles your document in the background, so you can see the results right away – great for learning quickly!
  • Find Errors Fast – writeLaTeX shows you errors & warnings as you go, so you can catch them early, and you don't have to find them in the LaTeX log.
  • LaTeX in the Cloud – Edit online from anywhere and on any device. There's no software to install, and no sign-up required.
Sounds exciting!

Monday, January 7, 2013

TeX Directory Structure or Where to Install Packages?

If you are a regular TeX user, there are many good reasons to understand and exploit the TeX directory structure (TDS). Let me offer some:

  • You download a package from CTAN (or somewhere else). This could be a style file, for example. Is there some central place you could put it, so that it is in the "path" (so that you can avoid making multiple copies of the same style file in all your working directories)?
  • You make a bibliography style file to suit a journal's requirements (using custom-bib). Can you put this in the "same" central place?
  • You have a bibliography (bib) file with all your references. Can you put this in the path too? And can you do that without completely cluttering that central place?
  • You have an account that is administered by a university or department, and the TeX installation is common to all users. Can you make sure that the .sty, .bst, .bib files that we talked about above do not get wiped off, when the systems administrator decides to upgrade the TeX installation?

If you use TDS, the answer is yes. The good news is that this works independent of the OS platform.

The first thing is to find out where this place is. You can do that by simply typing the following in a terminal.

kpsewhich -var-value=TEXMFHOME

On my Mac this is ~/Library/texmf, and on my Linux desktop it is ~/texmf.

If such a directory does not already exist, you can create it (mkdir texmf) at the above location. You then make sub-directories. The important ones are:

bibtex directory
   - bst sub-directory (put bst files here)
   - bib sub-directory (put bib files here)
tex directory
   - latex sub-directory (put packages here)
doc directory
     put doc files from packages installed in the tex directory here.

You can make further sub-directories.

For example, you can make a "tools" directory within texmf/tex/latex/tools and put, for example, the tools package files here (tex and sty), and move the corresponding doc files to the texmf/doc folder.

In newer systems you don't even have to run texhash or mktexlsr to "update" the path if you do this.

Thursday, November 15, 2012

Exporting Matrices in Octave/Matlab to LaTeX format

I often need to include vectors or matrices computed during GNU Octave sessions in my lectures or presentations. Here is a quick program called matrixTeX.m (stored on Google Drive) that takes in a matrix A and spits out the matrix form in LaTeX.

In the simplest form, matrixTeX(A), it simply takes in a matrix  A, checks whether the elements are integers or floats, and prints out to the screen in the appropriate format using the amsmath matrix environment bmatrix.

If you'd like the elements of the matrix displayed in a particular format, you can optionally use a second input argument to specify a C-style formatting string: e.g: matrixTeX(A, '%10.4e'), or matrixTeX(A, '%d').
In addition, you can also specify a third argument to specify alignment of columns as or matrixTeX(A, '%d','r'), where the 'r'  stands for right alignment. This option uses the bmatrix* environment provided by the mathtools package, which needs to be included in the LaTeX preamble.

Monday, September 17, 2012

How to: Crop PDF Images

If you use Matlab (or some other program) to make a plot and then export the figure to a PDF (for inclusion in a TeX document for example), you will end up with plenty of whitespace around the image as below.

It is easy to find out the "bounding box" using ghostscript as:

gs -sDEVICE=bbox -dNOPAUSE -dBATCH file.pdf

which after a bunch of messages yields something like:

%%BoundingBox: 89 231 491 545
%%HiResBoundingBox: 89.766138 231.192063 490.841915 544.769913

You can use those dimensions, and use the TeX command

\includegraphics[bb = 89 231 491 545 ]{file.pdf}

to trim the image within TeX.

Alternatively, you can also use ghostscript to crop the image itself by using:

gs -sDEVICE=pdfwrite \
   -o trimmedFile.pdf \
   -c "[/CropBox [89 231 491 545] /PAGES pdfmark" \
   -f file.pdf

where the output file trimmedFile.pdf is generated. You then don't have to worry about the bounding box inside the TeX document.

Another route is to use the perl script pdfcrop.

pdfcrop file.pdf

produces the appropriately cropped file file-crop.pdf



Monday, August 20, 2012

Aligning Matrix Columns in LaTeX

The standard amsmath packages provide a convenient interface to write matrices in LaTeX. For example, the following code produce the matrix:
\[\begin{bmatrix} 1 & -1 \\ -1 & 1  \end{bmatrix}.\]
Clearly the columns could do with some alignment. Of course, you could do it from scratch by using the array environment, but that would mean giving up the convenience of the AMS matrix environments.

Enter the mathtools package, which is an add-on to the amsmath packages. The mathtools package loads the amsmath packages, if they haven't already been.

Once you've loaded the mathtools package (using the usepackage command), you can specify the alignment of your matrices with a special starred environment as follows:
which yields:
Voila!


Monday, August 13, 2012

TeXcount: Number of words in a LaTeX document

Since TeX is really a markup language, counting the number of words in a document is tricky. Obviously, you don't want to literally count words in tags like \chapter{}, \begin{center}, \cite{reference1, reference2} etc.

You may have macros, which need interpretation.

You may have external files that you are collecting together in a master document by using \input{} etc.

In short, it is not as simple as it seems.

You could try to use front-end programs like kile or TeXShop which will give you a simple total count. My front-end program of choice --- TeXMaker --- does not do it for me.

If your document is very simple, you could try to "detex" the LaTeX tags, and use a simple Linux utility like "wc".

The best solution seems to be TeXcount.

There is a web-interface that lets you paste your TeX document in a web-form.

Alternatively you can download the script. It is essentially a small perl program (400kB download in all, the actual script is about 90kB) called texcount.pl, which you can run quite simply as

perl texcount.pl filename.tex

Here's the form (default) output it spits out


Encoding: ascii
Words in text: 10324
Words in headers: 81
Words in float captions: 219
Number of headers: 30
Number of floats: 5
Number of math inlines: 198
Number of math displayed: 18
Subcounts:
  text+headers+captions (#headers/#floats/#inlines/#displayed)
  14+9+0 (1/0/0/0) _top_
  89+1+0 (1/0/0/0) Section: Introduction
  419+2+44 (1/1/0/0) Subsection: Analytical Rheology
  646+2+38 (2/1/3/0) Subsection: Polymers
  236+3+0 (1/0/0/0) Subsection: Scope and Organization
  35+3+0 (1/0/0/0) Section: Motivation and Background
  355+2+19 (1/0/2/0) Subsection: Linear Polymers
  657+2+24 (1/1/17/1) Subsection: Branched Polymers
  205+3+45 (1/1/0/0) Subsection: Model-driven Analytical Rheology
  97+6+0 (1/0/0/0) Section: Models for Polymer Dynamics and Rheology
  597+2+0 (1/0/2/0) Subsection: Historical Development
  872+5+25 (2/1/8/0) Subsection: The Tube Model
  211+4+0 (1/0/0/0) Subsection: State of the Art
  273+2+0 (1/0/5/0) Subsection: Computational Models
  162+3+0 (1/0/0/0) Section: Methods and Progress
  1774+5+0 (3/0/108/15) Subsection: Linear Polymers
  2955+24+24 (9/0/51/2) Subsection: Branched Polymers
  727+3+0 (1/0/2/0) Section: Summary and Perspective


You can exercise significant control over the way it parses the document and reports the results by using options that are described in the manual.

Tuesday, June 5, 2012

TeXmaker: Setting up user tags

Texmaker is a really nice LaTeX editor, and has become my default choice. It has many things to recommend itself, including the fact that it is platform independent, has auto-complete (commands, references, and citations), is easy to configure and use etc.

A particular feature that I have grown to depend on is command shortcuts. Texmaker allows the user to define keyboard shortcuts to commands and environments, by going to User > User Tags > Edit User Tags.

Simple commands are quite easy. For example, if you find yourself using the command "\pause" (in beamer presentations) very frequently, you can go to User > User Tags > Edit User Tags, pick a function key (1-10 - say "1"), enter "\pause" in the LaTeX content box. Pressing Shift+F1, automatically inserts \pause at the cursor.

Let us say you have a lot of vectors in your equations, and you like to bold-face your vectors with \mathbf{}. You can create a shortcut to this by saying "\mathbf{@}" in the LaTeX content box. When you press Shift+FX (X=1-10, as appropriate), you get the \mathbf{} tag, and your cursor lies between the curly braces. You can also select some text and press Shift+FX and the \mathbf{} tag does what you would like/expect it to to.

You can also define shortcuts to environments. Say you use the "\begin{frame} \end{frame}" environment a lot. All you do is say "%frame" in the LaTeX content box.