Thursday, February 23, 2012

Centering Individual Cells in Tabular Environment

Sometimes it looks good to center the header of a table, but not the content.
I used to use minipage to do this, but using multicolumn seems to work better

\begin{table}
\centering
\begin{tabular}{rlr||rlr}\hline
\multicolumn{1}{c}{Number} & \multicolumn{1}{c}{Return Name} & \multicolumn{1}{c||}{Degree}
& \multicolumn{1}{c}{Number} & \multicolumn{1}{c}{Return Name} & \multicolumn{1}{c}{Degree}\\
\hline
17 & Russell 3000 & 9 & 17 & Russell 3000 & 14 \\
19 & MSCI EAFE & 9 & 19 & MSCI EAFE & 12 \\
20 & MSCI EM & 6 & 4 & CombinedEnergy DS SC1 & 9 \\
3 & Growth DS & 5 & 20 & MSCI EM & 9 \\
25 & Bacrclay's HY SC3 & 5 & 7 & HF1 Distressed DS & 8 \\
4 & CombinedEnergy DS SC1 & 4 & 25 & Bacrclay's HY SC3 & 7 \\
2 & Ventures DS & 3 & 1 & Buyouts DS SC1 & 6 \\
\hline
\end{tabular}
\end{table}

Sunday, February 12, 2012

tikzDevice and knitr

Ubuntu's late packages are based on texlive 2009.
Latest is texlive 2011; so I updated to it but I started getting this error

Error in get("tikz", envir = as.environment("package:tikzDevice"))(..., : Graphics API version mismatch

Error was due to tikzDevice package being older than what knitr package expects
To fix this problem, install the repository version of tikzDevice

install.packages( 'tikzDevice', repos = 'http://r-forge.r-project.org' )

After that all is good. (ref thread)

Friday, February 3, 2012

Multiple Comparison in Bash

Checking multiple conditions in Bash can be tricky.

Following require STRING1 and STRING2 to be not empty and COUNTER < MAX.
if [[ -n "$STRING1" && -n "$STRING2" && $COUNTER -lt $MAX ]];
then
...
fi


This post is a nice concise resource.

"knitr" and "ggplot2" for Automatic LaTeX Report Generation with R

knitr is supported under R versions >2.14.1. So I upgraded R, and also needed some extra packages for ggplot2. (probably not all are required for ggplot2 but I am interested in using these packages in my research)

sudo apt-get install libX11-dev libglu1-mesa-dev libxml2-dev libgeos-dev libproj-dev libgdal-dev

Some R packages as well

install.packages(c("rgdal","maps","maptools","rgeos"))

Only, then, was I able to install knitr package according to this post.

install.packages("devtools")
library(devtools)
install_github("ggplot2","hadley")

install_github("knitr","yihui")

Package management with "apt"

Useful information can be found here.
  • Search: apt-cache search [package name]
  • Package info: apt-cache show [package name]

Thursday, February 2, 2012

Using pgfSweave Package for R

pgfSweave can generate a latex document with R code in it. This means that plots are updated automagically, but setting up/getting used to it can be a big pain. The difference between pgfSweave and Sweave is that latter cannot cache plots. Hence, if one uses packages like ggplots, the report generation can be much slower than using pgfSweave.

To use pgfSweave, start with putting this in the preamble
\documentclass{article}

\usepackage{tikz}
\tikzset{external/system call={latex \tikzexternalcheckshellescape -halt-on-error
-interaction=batchmode -jobname "\image" "\texsource";
dvips -o "\image".ps "\image".dvi;}}

\begin{document}

<<setup,echo=F>>=
setCacheDir("cache")
options(keep.space=TRUE)
@

<<data,cache=T,tidy=T>>=
# hey, a comment
data(airquality)
print(kruskal.test( Ozone ~ Month, data = airquality )) # and another
@

\begin{figure}[!ht]
\centering
%notice the new options
{\pgfkeys{/pgf/images/include external/.code={\includegraphics[width=6in]{#1}}}
<<boxplot,cache=T,echo=T,fig=T,width=3,height=3,tikz=T,external=T,highlight=T>>=
boxplot(Ozone ~ Month, data = airquality,
main='Ozone distribution',xlab='Month',ylab='Concentration')
@
}% this brace ends the effect of 'include external'
\caption{This is from pgfSweave. Text is typset by \LaTeX\ and so matches the
font of the document.}
\end{figure}

\end{document}


Following inserts a cleaned up (and highlighted) code block whose result is cached <<data,cache=T,tidy=T>>=
# hey, a comment
data(airquality)
print(kruskal.test( Ozone ~ Month, data = airquality )) # and another
@


Following inserts a plot of size 2x2 sq.in. and embeds it at size 3x3 sq.in. \begin{figure}[!ht]
\centering
%notice the new options
{\pgfkeys{/pgf/images/include external/.code={\includegraphics[width=3in]{#1}}}
<<boxplot2,cache=T,echo=T,fig=T,width=2,height=2,tikz=T,external=T,highlight=T>>=
boxplot(Ozone ~ Month, data = airquality,
main='Ozone distribution',xlab='Month',ylab='Concentration')
@
}% this brace ends the effect of 'include external'
\caption{This is from pgfSweave. Text is typset by \LaTeX\ and so matches the
font of the document.}
\end{figure}


The Makefile from pgfSweave package shows how to compile things.
R CMD pgfsweave --pgfsweave-only pgfSweave-latex-example.Rnw
latex pgfSweave-latex-example.tex
make -f pgfSweave-latex-example.makefile
latex pgfSweave-latex-example.tex
dvipdf pgfSweave-latex-example.dvi


This post was useful.

LaTeX Equations on Blogger

  1. In the sidebar, locate the Layout button
  2. Locate the HTML/Javascript Gadget
  3. In the Pop-up window, leave "Title" empty and copy-and-paste this code according to this post:
    <script src=\"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML\" type=\"text/javascript\"> </script> 

    Or this, if you want the secure https protocol to fetch the javascript file.
    <script type=\"text/javascript\" src=\"https://d3eoax9i5htok0.cloudfront.net/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML\"> </script>
  4. Now LaTeX equations can be written by $$a \neq b$$ or \[a \neq b\] for equation block, and $a \neq b $ for inline equations

    For example,
    $$ \sqrt{n}\left( \frac{1}{n}\sum_{i=1}^n x_i - \mu \right)\ \xrightarrow{d}\ \mathcal{N}(0,\,\sigma^2) $$
    yields $$ \sqrt{n}\left( \frac{1}{n}\sum_{i=1}^n x_i - \mu \right)\ \xrightarrow{d}\ \mathcal{N}(0,\,\sigma^2) $$

Apply Commands Only to All Files or Directories

Choose "type" to be "d" (directory) or "f" (file).
find . -type "type" -exec chmod 755 {} \; 
find . -type "type" -exec chown owner.group {} \;

More generally, any "command" will work, too.
find . -type "type" -exec "command" {} \;

Wednesday, February 1, 2012

Running Commands in a Sub-shell

Content is from this post

Here is how to run "/bin/script.sh" inside "/workingdir" without changing your current path.
(cd /workingdir; /bin/script.sh)

Here is how to run sequence of commands with checking status after each command by inserting "&&"
(cd /workingdir && /bin/script.sh)

Memory is better managed by using "exec" command.
(cd /workingdir && exec /bin/script.sh)