Monday, April 23, 2012

How to Easily Use OpenType Fonts in LaTeX

I became interested in LaTeX out of a desire to be able to produce high-quality PDFs for self-published books. Someday I hope to be able to produce books of comparable quality to these humanities books typeset in TeX. This idea became even more feasible when I discovered the text content could be written in Markdown and converted to LaTeX with pandoc (More information in this article).

Typographically, the example books I linked to above are more the exception than the rule: the vast majority of LaTeX documents use the same boring default font, Computer Modern, that was originally packaged with the software in the 1980s. Using Computer Modern in a self-published book would be almost as bad as using Times New Roman or Arial.

If you try to figure out whether and how you might be able to use your computer’s normal fonts with LaTeX, you will soon come across a lot of extremely complicated and incomplete documentation about how to convert TrueType or OpenType fonts into a format LaTeX can use.

The happy truth is that these instructions are now obsolete: you now have easy access to OpenType fonts on Windows and Mac platforms, thanks to a new version of LaTeX called XeTeX. XeTeX includes a package called fontspec that gives full access to all system fonts, as well as advanced features for OpenType fonts, such as ligatures and small caps. XeTeX is available for Mac, but what most people don’t say is that this font-accessing goodness can also be used on Windows since XeTeX is included with Windows distributions such as TeX Live and MikTeX.

That being understood, here’s how to use your system fonts in your TeX documents (source):

  1. Use the xelatex command in place of pdflatex
  2. Add \usepackage{xltxtra} at the beginning of your preamble (some XeTeX goodies, in particular it also loads fontspec, which is needed for font selection).
  3. Add \setmainfont{Name of OTF font} in the preamble.
  4. No step 4.

Note: If you are using the aforementioned pandoc to generate your TeX documents, you do not need to do step 2 — pandoc already includes the fontspec package in its default template. Also, you can set the main font by adding the option --variable=mainfont:"font name" when calling the pandoc command.

Thursday, April 19, 2012

Publish multiple Markdown files to HTML in Windows

I wrote this script as a means of setting up a dead-simple “knowledge base” in HTML format.

The idea is to write documentation as a collection of plain-text files in Markdown format and have a no-fuss way to publish them as HTML, re-publishing changes as necessary.

In order for this script to work, you need to be on Windows, and you need to install a program called pandoc.

How to use it:

  1. Save a copy of this script file in any folder containing a bunch of Markdown-formatted text files. Include a stylesheet.css file in this folder as well if you want the HTML files to have CSS styling.
  2. Run the script (double-click it) — it will silently create updated HTML files for every text file in the folder. Only text files whose HTML counterparts are out of date or nonexistent will be processed.

You can either copy and paste the code below into Notepad and save it as a .vbs file, or you can download the latest version in a zip file. The code in the download will be more extensively commented, and may also contain enhancements developed since this post was written.

Here’s the basic code (provided under the terms of the Artistic License 2.0 — http://www.perlfoundation.org/artistic_license_2_0):

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strThisFolder = objFSO.GetParentFolderName(Wscript.ScriptFullName)
Set objStartFolder = objFSO.GetFolder(strThisFolder)
strConverterCommand = "pandoc -f markdown -t html -c stylesheet.css -o "

Set objFilesToUpdate = CreateObject("Scripting.Dictionary")

Set colFiles = objStartFolder.Files
For Each objFile in colFiles
    If objFSO.GetExtensionName(objFile.Name) = "txt" Then

        ' Check if HTML version of this text file exists in this folder
        strHTMLName = strThisFolder & "\" & Replace(objFile.Name, ".txt", ".html")
        If objFSO.FileExists(strHTMLName) Then

            ' If it exists, compare the timestamps
            Set objHTMLFile = objFSO.GetFile(strHTMLName)
            If objFile.DateLastModified > objHTMLFile.DateLastModified Then
                'If the text file is newer, add this text file to the list
                objFilesToUpdate.Add objFile.Name, strHTMLName
            End if

        Else
            ' If the file does not exist yet, add this text file to the list
            objFilesToUpdate.Add objFile.Name, strHTMLName
        End if
    End if
Next

' Update all the text files in the list.
colFilesToUpdate = objFilesToUpdate.Keys
For Each strSourceFile in colFilesToUpdate

    objShell.Run strConverterCommand & objFilesToUpdate.Item(strSourceFile) & " " & strSourceFile, 3, True
Next

Possible Future Improvements:

  • The script isn’t very helpful about telling you how long the process is going to take. I looked at several options for providing a progress bar or some kind of status output, but ultimately VBScript is just really sucky at this.
  • Pandoc is a very powerful converter. One could easily tweak the script to add options for producing LaTeX or even PDF files.

Tuesday, March 13, 2012

Retro click-beep sound from InstaGamer - now an iPhone text alert tone

I just downloaded the new free iOS game InstaGamer and its retro click-beep snippet of a sound effect is just exactly what I’m looking for in a text alert tone. So I made it into a ring tone for the iPhone. Download it here, then load it into iTunes and sync your phone.

Backstory: I’ve long been in search of the perfect “text alert” tone for my iPhone. The ones that come with the iPhone all seem too long; to me, a good text alert tone lasts a half second or less. You potentially end up hearing it dozens to hundreds of times a day, so all you need is a slippy little audio cue, not a prolonged gag or music riff.

(Play the game too, it's fun.)

Monday, March 12, 2012

Formatting and Typesetting your Book in MS Word

Format your page and text block

It starts with your page size. If you are self-publishing with a service like Lulu or CreateSpace, you select the size of your book, and this will give you a set of constraints (margins, etc.) to start with. Your page size and maximum margins will generally determine roughly how wide your text block can be.

Next, you want to find a matching set of values for your final font size, line height, and line width. The method I’m going to give you here is based on the fact that there is a set of ideal proportions between font size, line height, and line width, that give maximum readability and aesthetic appeal. See this article about golden ratio typography for more information.

  1. Take the width of your text block in inches and multiply by 72. This tells you how many “points” there are in one line of text.
  2. Take the square root of this number and divide by 1.618 (the golden ratio). This gives you an optimal font size, in points, for your main text.
  3. After rounding this font size to within half a point, multiply it by 1.618 again. This will give you your optimal line height in points.

You may need to reiterate a few times until you get a matching set of numbers that fit well on your page and do not need too much rounding.

Configure Word’s typesetting

You can actually get book-quality typesetting from Word if you just change a few options.

A couple of points about paragraph formatting:

  • Make sure your main text is set to “justified”, and not “flush left.” While it is currently better to set text flush-left on the web, books look better justified and are nearly always set that way.
  • Don’t have spaces between paragraphs in your main body text.
  • Paragraphs should have a first-line indent of the width of about 2 or 3 characters, but only where two or more paragraphs are joined together: the first paragraph of any group of paragraphs should have no first-line indent. (This could be a pain to manage; I tend to handle it by making all paragraphs indented by default, then going back and manually removing the indent from first paragraphs at some later stage in the editing.)

Make sure the following settings are enabled under the “Compatibility” options. To get to these options, click on the circular “Office Button” in the upper left corner, and then click Word Options at the very bottom of the menu. Then click Advanced, scroll down to the bottom of that section, and click the + next to Layout Options. On older versions, click the Tools menu, then Options (or EditPreferences on a Mac) then click the Compatibility tab.

  • Put a check next to “Do full justification like WordPerfect 6.x for Windows.” This allows justified text to contract as well as expand, making the automatic adjustments look a lot better.
  • “Don’t add extra space for raised/lowered characters.”
  • “Don’t expand character spaces on the line ending SHIFT+RETURN.” This ensures that lines you end with a soft return will still be properly justified.
  • “Suppress ‘Space Before’ after a hard page or column break.”

Configure Word’s hyphenation settings. On the toolbar, click the Page Layout tab, then Hyphenation drop-down button → Hyphenation Options. (On older versions, click Tools menu → LanguageHyphenation.)

  • Put a check next to “Automatically hyphenate document.”
  • Set “Hyphenation zone” to about half an inch.
  • Set “Limit consecutive hyphens” to 3.

Finally, enable ligatures. If your font is of good quality and has alternates for character combinations like fi and ffi, this will tell Word to use them automatically. This can only be done in MS Word 2010 or later.

  • Open the Font settings window. You can do this by selecting some text, right-clicking and selecting Font from the menu, but I recommend setting this up in whatever “style” you use for your body text (e.g., “Normal”): Home tab, right click the style, select Modifiy, then Format button → Font.
  • Click the Advanced tab, then next to the Ligatures option, select Standard Only.

Sources

These tips are compiled from many sources, and in many cases updated for clarity or accuracy with newer versions of Word.

Thursday, March 8, 2012

WriteMonkey: Tips and Tricks for Writers

I use WriteMonkey for almost all my writing. It’s the best Windows-based text editor I have found for writing prose (as opposed to programming code).

WriteMonkey is extremely Markdown-friendly — useful if, for example, like me you write all your blog posts in Markdown format.

Not all of WriteMonkey’s features are well-explained or documented, so I’m writing them up here.

Configure Markdown features

  • Markdown highlighting will not work unless you have Markdown set as your “Markup Standard” — set this in the Print & Export section of your Preferences screen.

  • Set the font size/weight/style used on headers: in the Preferences screen’s Colors & Fonts tab, click the button labeled ... in the upper right section (why they didn’t label it more clearly is beyond my understanding).

  • Make your exports look great. Download the template in this zip file and place it in WriteMonkey’s templates folder. It’s a version of this Markdown stylesheet with the following changes:

    • Removed padding: 0; margin: 0 rule for the ul and ol elements - this preserves indentation in multi-level lists.
    • The max-width was widened to look better on bigger screens.

Some undocumented features I found by accident

  • You can toggle whether WM will use normal quotes or “smart quotes” with CTRL+SHIFT+' (apostrophe).

  • Out of the box: type /now to insert the timestamp. You can format this timestamp in the Preferences screen.

Use WriteMonkey to write your book

WriteMonkey has a number of great features for writers:

  • It lets you set and monitor progress goals for your writing based on either word count or time or both.
  • Hit F5 to toggle between your main text and the “repository,” which works as kind of a scratch pad for the current file.
  • You can use the Jump screen to set navigate around your text’s headings, bookmarks, and todo items.

The upcoming version (2.3.5.0 as of this writing), however, will have some great project management functionality. (See here for more info)

  • Folders will be treated as projects, and all the files within it will be part of the project. You’ll be able to switch quickly between text files in the same folder using a new Files view in the Jumps window.
  • You’ll be able to quickly merge all of a project’s files into a single text file.
  • You’ll be able to mark a file with “tags” using a comment line (starting with \\) at the top of the file, and filter the project file list by tags.
  • Special tags affect how the file is treated in the project window
    • Tagging a file with a color name will cause that file to show up with a colored star in the jump screen. Multple colors mean multiples stars, e.g. // red red red will add three red stars.
    • Adding the “draft” tag will move the file to the “repository section” — the file will be presented with lighter color and excluded from total word count.
    • Tag with a percentage, e.g. // 50% to add a grey progress bar
    • Tag with a date in order to add a deadline; the border of the file will turn red when it becomes past-due

Let us know of any additional tips in the comments!

Friday, February 17, 2012

Siri: Slow, unreliable, and maybe not a priority at Apple

Siri had a lot of promise when it was released as a feature of the new iPhone 4S. It wowed everyone at the WWDC keynote in October 2011.

I’ve had an iPhone 4S since November 2011, and I would describe Siri as gimmicky, flaky, and useful only within a narrow subset of the things it was promised for.

The biggest killers of my enthusiasm for using Siri are laggy interaction and flaky availability. Interaction misses plus plain old network lag combine to make Siri slow, slow, slow.

There are five possible outcomes to each Siri interaction:

  1. Siri misunderstands what you said due to you speaking incorrectly, or
  2. Siri misunderstands what you said because of a voice recognition error, or
  3. Siri understands what you said but misunderstands what you meant, or
  4. Siri is unable to connect to its servers, or
  5. Siri connects and responds correctly

Four out of these five outcomes result in a failed interaction. While you can get better at speaking correctly, there are few to no workarounds for the other three error conditions.

Since Siri goes out over the internet in order to analyze each spoken interaction, it can often take a good 5-10 seconds to offer a response to what you’ve just said1. In addition, at least every other day Siri’s servers seem to go dark for a period of time, resulting in “really sorry, but I can’t take any requests right now, please try again in a little while.”

Add this delay, and a random but sizeable chance of connection failure, in between each incorrect response, and you soon get used to doing most things the old way rather than gamble on an unreliable Siri.

Someone’s bound to respond that Siri is still officially beta software. I am aware of that, and don’t expect it to be perfect2.

But it’s also worth noting that Apple, like most software companies, has introduced many apps and features that they then failed to improve upon or even abandoned3. My gut feeling is that Siri is in danger of ending up as the next Mobile Me, especially given that it has received almost no improvement or attention since its release so far.


  1. Connection failures are especially common when you are on the outer edge of a known wifi network, such as when in the driveway or parking lot, or walking to/from your house. The iPhone seems to think it is connected via wifi for some time after you have left range, causing Siri to attempt using a nearly non-existent connection. 

  2. Although I do expect beta software to be at least feature-complete, which I don’t believe Siri is. 

  3. See the Calendar, Weather, Music and Stock apps, iTunes Genius, Ping, and of course Mobile Me. 

Friday, February 3, 2012

Recommended Calendar Apps for iPhone

The calendar app that comes with iOS is plain, and has many shortcomings: the interface for creating new appointments is clumsy, the colour-coding is not visible in month view, and there’s no week view at all, to name a few.

Here are some notes on popular calendar apps I’ve tried and can (mostly) recommend.

  • Calvetica Classic is what I currently use and recommend for iPhone. As far as I can tell, it’s identical to Tempus (made by the same developer) but for some reason costs less. (Tempus may be the one that gets more maintenance attention from the developers in the future, however). Calvetica Classic’s interface is designed to be quick and easy to use, and to look attractive. It also happens to be reasonably prompt about automatic syncing — which, surprisingly, not all apps are (see below).

    The reason for the “Classic” version is that the makers of Calvetica made a New-Coke-style error when they redesigned and rewrote the app for the new version 4.0. The regressions in the interface and buggy experience of the new version apparently left a lot of users clamouring for the old version, and from what I can tell, the developers responded by offering both Calvetica Classic and Tempus. I haven’t used the new version, but the reviews on the App Store currently show that it still needs a good bit of bug fixing, especially on the iPad.

  • The other really good option out there is Week Calendar. I haven’t tried it personally, but this one has been recommended by Lifehacker, and has hundreds of great reviews (many more than any version of Calvetica), so it must work well. The only reason I didn’t go for it was that Calvetica is faster for adding events1, and Calvetica just looks more cool and elegant.

  • Agenda was recommended recently on Daring Fireball and was actually the first calendar app I used after getting our phones’ sync configured. It’s not as quick to use as Calvetica[^1], but it looks gorgeous, and swiping between month/week/day view is almost too good to give up. The app otherwise great except for one major thing: it’s terrible at automatic calendar syncing, especially when an event on a shared calendar is being deleted. In my experiments Agenda would not automatically sync until I had either shut down the app and restarted it, or until I had opened the default Calendar app (which triggers its own sync). You can also use the “shake phone to sync” feature of Agenda, but honestly, all three of these things are ridiculous.


  1. Note that if you have an iPhone 4S, you can set up new events very easily on your default calendar with Siri, no matter what app you use.