Thursday, March 31, 2011

Latex Math Tips

Summations
  • If summations become cramped, use \displaystyle before \sum.
  • If summations should be cramped, use \textstyle before \sum.

Numbering lines of equations using align
  • Use align to number all lines; use align* to switch off numbering
  • In align, use \notag before \\ to switch off numbering for a line
  • In align*, use \tag{...} to number (or rather tag) a line
  • Given a numbered/tagged line, use \label{...} before \\ to refer to the line using \ref{...}.
  • Use \allowdisplaybreaks to allow a single equation block to be split over two pages.
Canceling terms in expressions
  • \usepackage{cancel}
  • Four options: \cancel{...}, \bcancel{...}, \xcancel{...}, \cancelto{<val>}{...} (in math mode)
  • Use \usepackage{ulem}, \sout{...} to strike out text.
Others
  • Use \boxed{} to put a box around equations(to highlight them). Note: If you use it within the align environment, it should not contain the `&'; otherwise it will give an error.
  • Use \newcommand{\myfrac}[2]{\ensuremath{\frac{#1}{#2}}} to define new commands (\myfrac) with many (2) arguments, and specify replacement latex code (\ensuremath...), including references to the arguments (#1, #2, ...). The new command can now be used in normal latex code (\myfrac{a}{b}). Aside: \ensuremath{} forces its contents to be interpreted in a math environment.

Wednesday, March 30, 2011

Thursday, March 24, 2011

Using xargs to redirect to command line arguments

To delete all .bak files in a directory, do
rm *.bak or ls *.bak | xargs rm
Essentially, rm is executed once with the whole of the piped input as argument.

To move the .bak files to another place, do
mv *.bak diffdir or ls *.bak | xargs -I var1 mv var1 diffdir.
Here, mv is executed separately for each file listed by ls. var1 is a placeholder. It is replaced each time by a file, and mv is executed.

To rename the .bak files to .bakup files, do
ls *.bak | xargs -0 -I {} mv {} {}up.
{} is another way of specifying a placeholder. -0 takes care of white space anomalies in the piped input.

Friday, March 11, 2011

Vim Tips

  • To beautify code (or pretty print, or auto indent), type ESC gg = SHIFT-G. This means:
    • ESC --> back to mode
    • gg --> go to first line
    • = --> indenting
    • SHIFT G --> to end of file.
(Courtesy snipplr.)
  •  To search and replace starting from the cursor type ESC :.,$s/foo/bar/gc. This means:
    • ESC --> back to mode
    • : --> command mode
    • .,$ --> from current line (.) till (,) end of file ($)
    • s --> substitute (foo with bar)
    • g --> all occurrences in a line
    • c --> confirm before replacing
 (Courtesy wikia.)