Showing posts with label LaTeX. Show all posts
Showing posts with label LaTeX. Show all posts

tikz.sty not found

May 12, 2015
Tested on: Linux Mint 17.1 Rebecca (Ubuntu 14.04 LTS - Trusty Tahr)

This error with pdflatex has been mystery to me for a long time. Surprisingly, it is not easy to find a clear solution online.
ERROR: LaTeX Error: File `tikz.sty' not found. --- TeX said --- Type X to quit or <RETURN> to proceed, or enter new name. (Default extension: sty)

Solution

sudo apt-get install texlive-pictures pgf
Installation of the package pgf is the most important bit! Without pgf LaTeX will keep complaining that tikz.sty is missing. This solution was also proposed in a comment on stackexchange:

... The pgf package, as I keep forgetting and remembering every year, is independently installed from the pictures (which includes tikz but it's not loaded unless pgf is present) extras, the common, and the main tex on 'ix systems.


Read more ...

A simple approach to reference and PDF management

Sep 5, 2014

Managing references and citations is essential for academic publishing and reproducible research. However, managing large volume of references and citations over years is no easy task! A number of softwares have been developed specially for the task and been discussed many times. In this post I will briefly describe my own approach, which I have been using over 4-5 years now, to manage references, notes and corresponding files including PDFs. In particular, my approach aims to be:

  • Scalable - Manage hundreds of references and related files; It should be easy to add new reference/notes and should require no/little maintenance.
  • Easy formatting - References should be accurate and should be able to handle different character encoding, accents and should adapt to any reference-style based on the publisher specification.
  • Portable - Easy synchronization of whole setup (files, notes etc.) to a new computer/laptop/device.
  • Searchable - Search through titles, authors, previous notes etc.
  • Cross Platform - Same method should work across different operating systems (Linux, Windows, Mac) with no/minimal changes.
  • Work offline - Because Internet access is never as reliable as one wants it to be (also see 'Portable' above).
  • Free and Open - "Free as in beer" and, preferably, as "free speech" as well (no proprietary format). Being "open" also make it possible to port the data to some-other format/method, if required.

Note that the described method relies on BibTeX, mainly because it can achieve all the above goals and because BibTeX (& LaTeX) is de facto standard for writing in my discipline (Engineering). Use of BibTeX also makes bibliography dead simple in LaTeX - absolutely no export/import required! If BibTeX is not acceptable, skip to last section below for some alternatives.

Approach

The approach itself is fairly simple and is motivate by The Unix philosophy of small tools. The approach has three components:

  1. A directory on our computer/device, say with name "papers", which will contain all the files related to the references. Basically, this directory is the self-contained repository of all our information.
  2. JabRef for managing the BibTeX (.bib) file which manages all information, and
  3. A file synchronization tool, like Dropbox or Unison, to synchronize the papers directory to other devices, if required.

So, basically we save our files (PDFs etc.) in a directory, open/read/edit the files using our favorite software, add/edit the reference information via JabRef, and sync to other devices using our favorite sync-software. (In theory one could also use any other BibTeX manager, but JabRef is absolutely the best BibTeX manager I know.) For ease of file management, we can create 2-3 sub-directories to organize files, but it is completely optional as JabRef allows more advanced categorization. The image on right shows a sample directory structure. reference.bib is the BibTeX file which contains all the information related to references and lies at the heart of this approach. The sub-directories contain PDF and other files related to references. In my approach, I use one single .bib file for managing all reference, which in my experience works in a much better unified-way than multiple .bib files.

Next, I will outline some bare-minimum steps to get started with this approach. However, feel free to explore JabRef options and make your own customizations.

Setup

  1. Create a papers directory with 1-2 relevant sub-directories. Optionally, throw in few PDF files of your references in sub-directories. Avoid spaces in file and directory names; use underscore instead.
  2. Download and install latest version of JabRef for your platform.
  3. Open JabRef and create new database (File->New database). Save database as a reference.bib in papers directory.
  4. In JabRef, go to the menu Options->Preferences and set following:
    • In Files tab, check "Open last edited databases at startup" and "Backup old file when saving".
    • In External programs tab, check "Allow file links relative to each bib file's location" and "Use the bib file location as primary file directory"
  5. Optionally, setup your synchronization/backup for the papers directory.

Adding references

This step is the most important step, as we want our reference to be accurate. Let us assume that we have read this paper from Einstein and have saved a copy of the PDF file in appropriate sub-directory. Next, we would want to add it to our refernce list and add some notes from our reading. The idea here is that we will be very careful with all the details while adding the references the first time and then we can use it confidently life-long (almost) without any changes. If you are entirely new with JabRef then go through this short tutorial to familiarize yourself.

  • Open reference.bib using JabRef. Go to BibTeX->New entry and then select Article from entry type dialog.
  • Fill in the details of the paper starting from "Required fields" tab. You need not fill in all the fields in other tabs. You can find more about fields in BibTeX Help and wikibooks.
  • You can also copy-paste the details of field from the paper, publisher website, or listing on Google Scholar. But make sure that the details are correct. For example, the author details of the Einstein paper is wrong on Google Scholar!
  • While selecting Bibtexkey be sure that it is unique as you would never want to change it in future (Bibtexkey is the key which would refer from LaTeX). I typically follow the format: firstAuthorLastName_year_four_word_paper_summary for my Bibtexkey (& PDF filenames).
  • To link the PDF (or any other file), go to "General" tab and add the link to file. You can add as many files as you want to an entry. This allows easy access to the files from JabRef. See help section for more details and options.
  • You can also add your review of the paper in "Review" tab. I prefer adding some "Keywords" and write my notes in "Comment" in "General" tab. Also, I prefer to use my favorite 3rd party tools to annotate/highlight the PDF itself.
  • JabRef also supports groups (which are somewhat similar to labels in Gmail) and marking for more advanced categorization. However, I never felt any need of such categorization because the search in JabRef works well and use of keywords pretty much categorizes all the references.

While adding new entries, JabRef will autocomplete author names and words and can also autogenerate Bibtexkey. Once you have added enough references, you can directly link to the reference.bib from LaTeX file for bibiliography. You can also put the .bib file in some sort of version control for enhanced security and control. I prefer to create weekly backup of the entire papers directory to external hard-drive for peace of mind.

Cross-platform

JabRef is written in Java which makes it cross-platform by design. I frequently use it on desktops (Linux, Windows and Mac). The method also works well with portable android devices with RefMaster. Basically, RefMaster replaces JabRef on andriod and it supports Dropbox integration. There should be similar apps for managing BibTeX on iOS platform.

Drawbacks

The only feature which I miss in this approach is the ability to search inside the linked files when searching in JabRef. However, it is not a show stopper, especially in presence of various desktop search tools.

Also, this approach will not parse your computer for PDF files and auto-magically generate "correct" references. This method is geared towards accuracy and, in my experience, the auto-magical metadata extraction from PDF do not work well frequently and have rather large error rate. Careful verification of reference data while entering data is important and fruitful in long run.

Another drawback could be the steep learning curve required for getting comfortable with LaTeX (& BibTeX). It is assumed that people from my discipline (Engineering) are already using or will be using LaTeX. See next section for alternatives.

Alternatives

If LaTeX (& BibTeX) is not your cup of tea then following are some alternatives (but one should consider why should they not use LaTeX and who else uses LaTeX). I ended up with JabRef because most of the following did not had upto-the-mark BibTeX management, atleast by my taste, when I was trying them.

  • Mendeley is a good alternative and specially works well with office documents and can search inside linked PDFs.
  • citeulike is an online service and works well for collecting references while browsing over internet.
  • Zotero used to be a browser-plugin which offers good features, but I never tried it seriously. The standalone version came much later but looks good.

There are several other softwares which can be used for refernce management. Here is a popularity context of different softwares and here is an irony post comparing them. Everyone has their own preferences and a solution may not be the 'best' solution for everyone. The described approach works for me and could be helpful to others.



Read more ...

LaTeX to word document

Jan 24, 2014
Tested on: Linux Mint 13 (Ubuntu 12.04), Microsoft Office 2010, LibreOffice 3

It is common to typeset scientific documents in  \(\LaTeX\). However, few people may like to still read and edit the document in word processors like Microsoft word (.doc or .docx)or LibreOffice (.odt). Here are quick (& dirty) steps to produce reasonably looking "office" documents from .tex files. The idea is to exploit HTML output as intermediate format which is readable by word processors. This ideas is shown below and has been explored and reported widely.
.tex.html.docx
  1. Convert \(\LaTeX\) to .html: There are various ways to achieve this, but htlatex from TeX4ht worked best for me. First install TeX4ht and then then run following on the \(\LaTeX\) document. Note that htlatex runs latex three times before calling TeX4ht programs and it would write out all the outputs in the same directory as the .tex file (see below for handling PDF images). The generated files would include one .html file (paper.html for command shown below). $ sudo apt-get install tex4ht $ htlatex paper.tex
  2. PDF images: If the .tex file uses PDF files as images, then htlatex (or latex) may not be able to go along with correct conversion. Some discussions on Stack Exchange were very help for this issue and provided the solution. First create a configuration file, say myxhtml.cfg, with following content: \Preamble{xhtml} \Configure{graphics*} {pdf} {\Needs{"convert \csname Gin@base\endcsname.pdf \csname Gin@base\endcsname.png"}% \Picture[pict]{\csname Gin@base\endcsname.png}% \special{t4ht+@File: \csname Gin@base\endcsname.png} } \begin{document} \EndPreamble Then run htlatex as shown below. Note that the .tex file should use correct extension for every included image (or else use \DeclareGraphicsExtensions{.pdf}). $ htlatex paper.tex myxhtml
  3. Fix HTML: Sometimes TeX4ht will not write out clean html files i.e. the html tags may not be aligned properly. This may prevent html to import into word processors. Online editors like Fix My Html and HTML Tidy Online did the trick for me. Use the .html file generated by htlatex as input to these online editors and save the fixed html file for next step.
  4. Import HTML in word processor: Usually most word processors would support importing HTML files. If that does not work (like in my case), you can just open the HTML file in a browser then copy-paste the whole page into the word processor (check if your word processor also supports Paste Special for formatted text. It everything works out then you should be able to just save the pasted document in .doc, .docx or .odt format. 
The above set of steps produced good looking single column document for me with correct bibiliography references. The math equations are embedded as images in the documents, which may not be optimal but the process works for reviewing and editing the text by someone who prefers word processors.
Read more ...

BibTeX hacks

Dec 27, 2013
Sometimes \(\LaTeX\) compilation can give fairly unhelpful error messages like these: ! Missing $ inserted. $ l.190 ... pp. 225--236, 10.1007/978-3-642-31340-0_19 18. [Online]. ? If these error message are in .bbl file, when you are using BibTeX (.bib file) for bibliography then it is possible that these error messages are result of some special characters in your .bib file. I have seen it frequently that some DOI links contain some special characters. Following are some ways to get around these issues so that resulting document is rendered correctly.
  • Escape underscore (_) with a backslash (\). In the above example, the original DOI url was 10.1007/978-3-642-31340-0_19 which was resulting into errors. Escaping the underscore with backslash fixed the issue for me: 10.1007/978-3-642-31340-0\_19 (notice a backslash before _19 near the end).
  • Sometimes DOI urls would contain less-than (<) and greater-than (>) character. These characters do not result into errors during compilation but would be rendered incorrectly. Escaping these characters with backslash generally does not work and often result into errors similar to that shown near the top of the post. An example (fake) DOI url is 2/3<80::AID-HBM2>3.0 which was rendering upside down question mark and exclamation marks (probably because of issues with font encoding). I found that putting < in math mode helps to resolve this issue, i.e. replace with $<$ and $>$ so that new DOI url looks like 2/3$<$80::AID-HBM2$>$3.0.
  • Changing font encoding by adding following lines in \(\LaTeX\) preamble can also help to resolve above issue with < and >, as discussed here. \usepackage[T1]{fontenc} \usepackage{lmodern}
  • \(\LaTeX\) provides means to describe special characters like accents or umlauts using a special notation as described in following table hot-linked from official homepage. In addition, also check out this really great answer on stackexchange about accented letters.

Read more ...

Useful LaTeX tricks

Aug 3, 2013

\(\LaTeX\) word count

It can get difficult to keep track of accurate number of words in a \(\LaTeX\) document. TeXcount is a script for counting words in text and math equations in LaTeX documents. It can also generate very detailed html output with color-coded words types. TeXcount is part of Tex live and can be installed by installing texlive-extra-utils from Ubuntu repository. TeXcount also has a web interface. $ texcount document.tex FILE: document.tex Words in text: 6462 Words in headers: 48 Words in float captions: 274 Number of headers: 19 Number of floats: 4 Number of math inlines: 204 Number of math displayed: 8 texcount -v -html document.tex > word_count.html

\(\LaTeX\) diff

Usual diff programs may not do a great job of diffing two \(\LaTeX\) documents. Following are some of the alternative ways:
  • Use wdiff with colordiff, similar to this stackexchange question: wdiff -n old.tex new.tex | colordiff | less -R
  • Use latexdiff to determine differences and create a new LaTeX document with markup differences between two latex files. latexdiff can be found in Ubuntu repository and detailed description of usage can be found here. An example with beautifully typeset document is given below. latexdiff draft.tex revision.tex > diff.tex
  • Another alternative could be to follow a simple guideline: Always start a new sentence in a new line and do not put any line-breaks in till the sentence ends. This allows traditional diff/merge programs like meld etc to work in a much better way.

\(\LaTeX\) and git

It is useful to use a version control system, like git, with \(\LaTeX\) documents. Here are some useful tips while working with git:
  • Check git + LaTeX workflow for some great tips.
  • Use latexdiff (also see above) in smarter way:
    • latexdiff <(git show checksumHash:file.tex) file.tex > diff.tex
    • Git-latexdiff can also be used for more advanced operations, like documents involving multiple .texfiles.
    • Check stackexchange discussion for more solutions.

\(\LaTeX\) on blogspot.com

MathJax is awesome. It is very easy to install and get \(\LaTeX\) equations working. \[ J_\alpha(x) = \sum\limits_{m=0}^\infty \frac{(-1)^m}{m! \, \Gamma(m + \alpha + 1)}{\left({\frac{x}{2}}\right)}^{2 m + \alpha} \]
Read more ...

gs (GhostScript) cheat sheet

Aug 28, 2012
Cheat-sheet for versatile interpreter GhostScript. It can be used to tweak, convert, produce high quality Postscript and PDF files.

Convert postscript (ps) to pdf:

gs -q -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=fileout.pdf filein.ps

Merge ps and/or pdf:

gs -q -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=fileout.pdf filein1.ps filein2.pdf filein3.pdf

Extract page(s) from a ps or a pdf document:

gs -q -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dFirstPage=3 -dLastPage=3 -sOutputFile=fileout.pdf filein.ps

Embed fonts in a pdf:

gs -q -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=fileout.pdf -dPDFSETTINGS=/prepress -dEmbedAllFonts=true -dSubsetFonts=true -dCompatibilityLevel=1.6 filein.pdf

Convert images to pdf:

Check this post. Or else use ImageMagic's convert.

PDF optimization options

-dPDFSETTINGS=/screen (screen-view-only quality, 72 dpi images) -dPDFSETTINGS=/ebook (low quality, 150 dpi images) -dPDFSETTINGS=/printer (high quality, 300 dpi images) -dPDFSETTINGS=/prepress (high quality, color preserving, 300 dpi imgs) -dPDFSETTINGS=/default (almost identical to /screen)

Paper size options

-sPAPERSIZE=letter -sPAPERSIZE=a4 -dDEVICEWIDTHPOINTS=w -dDEVICEHEIGHTPOINTS=h (point=1/72 of an inch) -dFIXEDMEDIA (force paper size over the PostScript defined size) -gWIDTHxHEIGHT (page size in pixels)

Output devices:

-sDEVICE=pdfwrite -sDEVICE=ps2write -sDEVICE=png16m (24-bit RGB color) -sDEVICE=pnggray (grayscale) -sDEVICE=pngmono (black-and-white) -sDEVICE=pngalpha (32-bit RGBA color) -sDEVICE=jpeg (color JPEG) -sDEVICE=jpeggray (grayscale JPEG) -sDEVICE=epswrite (encapsulated postscript) -sDEVICE=txtwrite (text output, UTF-8) Check -sOutputFile below, for separate images for each page of a multi-page document. Tiff, PNM and many more formats are supported. Check details in Ghostscript output devices.

Help & list of available devices:

gs -h

Other options

-dNOPAUSE (no pause after page) -dBATCH (exit after last file) -sOutputFile=ABC-%03d.pgm (produces 'ABC-001.pgm'..'ABC-010.pgm'..) -dEmbedAllFonts=true -dSubsetFonts=true (Embeds only the characters used in document) -dCompatibilityLevel=1.4 (Adobe's PDF specifications, >=1.4 for font embedding, =1.6 for OpenType font embedding) -dCompressPages=true (compress page content) -dFirstPage=pagenumber -dLastPage=pagenumber -dAutoRotatePages=/PageByPage (or /All or /None) -rXRESxYRES (XRES & YRES in pixels/inch) -rRES (same XRES & YRES, affects images and fonts converted to bitmaps) -sPDFPassword=password More details & defaults on ps2pdf documentation.
Adobe® PDF Creation Settings - Used by GhostScript.
GhostScript Documentation.

Read more ...

AUCTeX on windows

May 13, 2011
It’s a little tricky to get AUCTeX working on windows, specially the preview feature working. Following steps helped me.
  1. Install MikTeX.
  2. Install Ghostscript & GSview.
  3. Install Emacs & AUCTeX from here.
  4. Add to system PATH the location of gsview32c.exe. (Required for LaTeX-preview). Restart the system (Did not work without restart for me)
  5. Add following lines to .emacs file (load "auctex.el" nil t t) (load "preview-latex.el" nil t t) .emacs file can be found in C:/users/username/appdata/roaming/ or in the home directory. 
  6. Change variable (see next section) preview-image-type to pnm. To use other types install correct dll files in bin directory of emacs.
Other customizations:
To customize/change a variables in Emacs use M-x customize-variable
  • Change default viewer: <to-find-out>
  • Change preview size: Customize variable display-mm-dimensions-alist. Experiment with width height. My setting: Width-250, height-180
Read more ...

Install style files in LyX

Sep 19, 2009

LyX is a frontend to LaTeX and so installation of any style file in LyX essentially mean: (1) installation of that style file in underlying LaTeX; and then (2) getting that style file work in LyX (using LyX layout files). Installation method for different types of style files are given below:

Install LaTeX class (.cls)
  1. create new directory with name <your-class-name> under /usr/share/texmf-texlive/tex/latex/
  2. copy <your-class-name>.cls file (& other files, if present) in the new directory
  3. sudo texhash
  4. create  a file <your-class-name>.layout in ~/.lyx/layouts
  5. edit the layout file according to the instructions given in Chapter 5 of "Customizing LyX: Features for the Advanced User" (Help->Customization in LyX).
  6. open LyX and click 'tools -> Reconfigure' and restart LyX when done. The new Document Class should be available now.
For Example, to install IEEEconf class file, download the zip files from here. Create .cls file by executing 'latex IEEEconf.ins'. Copy the folder IEEEconf to /usr/share/texmf-texlive/tex/latex/. Run sudo texhash. Find IEEEtran.layout file & copy it to a new file named IEEEconf.layout in ~/.lyx/layouts (IEEEconf and IEEEtran are very similar, so IEEEtran-layout file can be used as layout file for IEEEconf). Edit the the second line in IEEEconf.layout from
# \DeclareLaTeXClass{article (IEEEtran)}
to
# \DeclareLaTeXClass{article (IEEEconf)}
and it should turn up when you reconfigure LyX.
Install LaTeX style (.sty)

Style file (.sty) is an addition to class files (.cls) files and they provides special formatting rules which overrides the similar rules in class  files, if present. So each style file must be associated with a class file. Installation process is similar to that of class files (see previous section of this post). Layout files (step 5 above) have different format. Instructions are given in Chapter 5 of "Customizing LyX: Features for the Advanced User" (Help->Customization in LyX).

For Example, to install ACRA style file, download the files from here. Copy the file acra.sty to /usr/share/texmf-texlive/tex/latex/ACRA/. Run sudo texhash. Find article.layout file & copy it to a new file named acra.layout in ~/.lyx/layouts (acra.sty uses "article" class). Edit the the second line in acra.layout from
#  \DeclareLaTeXClass{article}
to
#  \DeclareLaTeXClass[article, acra.sty]{article (ACRA)}
Then add in next line

Preamble
    \usepackage{acra}
EndPreamble


and it should turn up when you reconfigure LyX.

Install BibTeX Style (.bst)
  1. create new directory with name <your-bib-style-name> under /usr/share/texmf-texlive/bibtex/bst/
  2. copy <your-bib-style-name>.bst file (& other files, if present) in the new directory
  3. sudo texhash
  4. open LyX and click 'tools -> Reconfigure' and restart LyX when done.
  5. open LyX and click 'tools -> TeX Information' and choose BibTex Styles. Click 'Rescan' The new BibTeX should be available now.

Read more ...