Syntax highlighting in terminal

Syntax highlighted cat

I was just quickly flipping through code on a terminal and got to thinking that it would be pretty handy to be able to syntax highlight when using head or grep without having to open an editor. Luckily, I remembered Pygmentize was pretty handy when syntax highlighting in LaTeX so I wondered if they had an equivalent output for terminal. It turns out they do (in standard or 256 color no less) so in a couple minutes I had a really short script for highlighting code at the terminal saved it to bin/ccat and was ready to go. Pygmentize really is impressive. Here's the script if anyone else is looking to do the same:

BASH:
  1. #!/bin/bash#!/bin/bash
  2. if [ ! -t 0 ];then
  3.         file=/dev/stdin
  4. elif [ -f $1 ];then
  5.         file=$1
  6. else
  7.         echo "Usage: $0 code.c"
  8.         echo "or e.g. head code.c|$0"
  9.         exit 1
  10. fi
  11. pygmentize -f terminal -g $file

It just looks for stdin and if it doesn't find it it looks for a file name or takes direct input. Obviously it requires Pygmentize (which is really easy to install if you already have Python).

Bash/UNIX
Programmer

Comments (7)

Permalink

Displaying Code in LaTeX

gioby of Bioinfo Blog! (an interesting read by the way) left a comment asking about displaying code in LaTeX documents. I've sort of been cludging around using \hspace's and \textcolor but I've always meant to figure out the right way to do things so this seemed like a good chance to figure out how to do it right.

LaTeX tends to ignore white space. This is good when you're writing papers but not so good when you're trying to show code where white space is an essential part (e.g. Python). Luckily there's a builtin verbatim environment in LaTeX that is equivalent to html's <pre>. So something like the following should preserve white space.

Code in LaTeX using verbatim
\begin{verbatim}
for i in range(1, 5):
  print i
else:
  print "The for loop is over"
\end{verbatim}

Unfortunately, you can't use any normal LaTeX commands inside verbatim (since they're displayed verbatim). But luckily there a handy package called fancyvrb that fixes this (the color package is also useful for adding colors). For example, if you wanted to highlight "for" in the above code, you can use the Verbatim (note the capital V) environment from fancyvrb:

Code in LaTeX using fancyvrb
\newcommand\codeHighlight[1]{\textcolor[rgb]{1,0,0}{\textbf{#1}}}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{for} i in range(1, 5):
  print i
else:
  print "The for loop is over"
\end{Verbatim}
Code in LaTeX using pygmentize

If you really want to get fancy, the Pygments package in Python will output syntax highlighted latex code with a command like: pygmentize -f latex -O full test.py >py.tex The LaTeX it outputs is a bit hard to read but it's not too bad (it helped me figure out the fancyvrb package) and it does make nice syntax highlighted output.

Here's an example LaTeX file with the three examples above and the pdf it generates if you're curious.

LaTeX
Programmer

Comments (8)

Permalink

How SINs (and Credit Card Numbers) Are Validated

A while back it was tax season in Canada and a friend of mine was trying to do his taxes online. But since he was foreign and didn't have a Social Insurance Number (their equivalent of an SSN), the helpful webapp wouldn't let him print the thing (of course it only informed him of that after he had already entered everything). We tried a few guesses before finally just using mine and crossing out my numbers after printing it. But I always wondered how the form knew our guesses were invalid. Luckily, I recently stumbled across a mention of how credit card numbers are validated.

It turns out SINs and credit card number are checked with something called the Luhn algorithm. Basically the algorithm just involves taking each digit, multiplying the second, fourth, sixth and so on digit from the right by 2 and adding up all the resulting digits (e.g. if 7 is multiplied by 2 then the resulting 14 is split into 1+4). If the sum of the digits is a multiple of 10, the number passes.

For example, to check 345678, you'd split it into 3,4,5,6,7,8. Then multiply 3, 5 and 7 by 2 to give 6, 4, 10, 6, 14, 8. Then split all the digits again to give 6, 4, 1, 0, 6, 1, 4, 8. That adds up to 30 so 345678 would be a valid credit card number (if there wasn't a set length).

Just for fun, here's a quick function in R to run the Luhn Algorithm on a number (or tell you the remainder so you can adjust):

R:
  1. luhnCheck <- function(number,returnLogical=TRUE){
  2.   numbers <- gsub('[^0-9]','',as.character(number))
  3.   numbers <- as.numeric(strsplit(numbers,'')[[1]])
  4.   selector<-seq(length(numbers)-1,1,-2)
  5.   numbers[selector]<-numbers[selector]*2
  6.   numbers[numbers> 9] <- numbers[numbers> 9] - 9
  7.   remainder <- sum(numbers) %% 10
  8.   if(returnLogical) return(remainder==0)
  9.   else return(remainder)
  10. }

So the next time some stupid web form needs a SIN number I'm going with 999999998.

Programmer
R

Comments (0)

Permalink

SAS Macros: Letting SAS Do the Typing

I've been meaning to write up a bit on using macros in SAS to complement my previous post on macro variables for quite a while. Luckily Norwegian guy reminded me about the pain of starting programming in SAS and provided me some motivation. So here's my take on using macros in programming.

Continue Reading »

Programmer
SAS
Statistician

Comments (14)

Permalink

WP_CodeShield

I've been burned a few times in WordPress when posting something like:

This is how you make text italic: This is italic.

when I actually wanted:

This is how you make text italic: <em>This is italic.</em>

Of course it's not too hard to remember to encode the special characters but this is WordPress and things are supposed to be easy. So I thought I would throw together a simple WordPress plugin to convert HTML special characters inside <code></code> tags. That was a few months ago and I got the plugin most of the way done and then got distracted with other things. But after reading about another person having the same annoyance, I decided I should finish the plugin.

Continue Reading »

Blogger
Programmer
Web

Comments (25)

Permalink