Preserving spacing in html code

Steve Simon

2005/05/06

[StATS]: Preserving spacing in html code (May 6, 2005).

I am starting to write a lot of web pages that show program code for R. I use the and tags most of the time, because this produces a fixed width font and preserves spacing. The default font on a web page is a proportional width font, which makes much of the indenting and spacing in a program look terrible. First, let’s look at what the program looks like in my text editor.

PreservingSpaces1.gif not found.

Here’s what the code looks like with the default font.

id1 <- as.character(c(6286:6294,7446:7458)) loc <- c( “UM”, “UM”, “UM”, “H”, “H”, “H”, “H”, “H”, “H”, “UM”, “UM”, “UM”, “UM”, “H”, “H”, “H”, “H”, “H”, “H”, “WU”, “WU”, “WU”) id2 <- c( “1589”, “1589”, “1589”,“18058”,“18058”,“18058”, “17869”,“17869”,“17869”, “1690”, “1621”, “1631”, “1566”,“18354”,“18381”,“18390”,“18401”,“18508”, “18535”, “0831”, “3881”, “5025”)

The first problem is that multiple spaces appear as a single space. You can replace the blanks with   before importing and that gets a bit closer to what you want,

id1 <- as.character(c(6286:6294,7446:7458)) loc <- c( “UM”, “UM”, “UM”, “H”, “H”, “H”, “H”, “H”, “H”, “UM”, “UM”, “UM”, “UM”, “H”, “H”, “H”, “H”, “H”, “H”, “WU”, “WU”, “WU”) id2 <- c( “1589”, “1589”, “1589”,“18058”,“18058”,“18058”, “17869”,“17869”,“17869”, “1690”, “1621”, “1631”, “1566”,“18354”,“18381”,“18390”,“18401”,“18508”, “18535”, “0831”, “3881”, “5025”)

but the proportional width font still causes problems. With the