Monday, October 6, 2014

Verbatim environment

Use the verbatim environment with small text (\small\begin{verbatim}) causes error in
the mdframe environment. To solve this, use the fancyvrb package with

\begin{Verbatim}[fontsize=\small]
\end{Verbatim}

Verbatim environment and mdframe package

When using an mdframed package to create a box around the verbatim environment,

\surroundwithmdframed[style=bluebox]{Verbatim}

the text following the environment becomes white. To fix this, use the fancyvrb package
and set the box to the Verbatim environment instead:

\usepackage{fancyvrb}
\surroundwithmdframed[style=bluebox]{Verbatim}

Thursday, July 24, 2014

Fixing Latex Errors


- Using \begin{table*} in a single-column document will cause "Incomplete \iffalse:" error.
- Use {\small \begin{verbatim} \end{verbatim}} will cause "Missing } inserted." error.
  Fixed by using package fancyvrb :
  \begin{Verbatim}[fontsize=\small]
  \end{Verbatim}

Fix \labelindent already defined error when using enumitem package and IEEEtran class

Insert
\let\labelindent\relax
before calling \usepackage{enumitem}

Tuesday, July 1, 2014

Setting up local texmf tree

1. Use the command
sudo tlmgr conf texmf TEXMFHOME "~/Library/texmf:~/remote/shared/texmf"

The default place for this file is
/usr/local/texlive/<year>/texmf.cnf

2. Try running kpsewhich -var-value TEXMFHOME to see where
TeX thinks your local texmf tree should be.

Create Beamer slides with presenter notes.

1. Add the following lines in Latex header:

\usepackage{pgfpages}
\setbeamertemplate{note page}[compress]  %% plain or default
\setbeameroption{show notes on second screen=left} % right = Content, left = notes

The option "left" will create a note page while the option "right"
will create a presentation slides.

2. In the beamer source, use \note{ xxxx your content} in each frame.
Only itemize environment can be used.

3. Create two pdf files, one is the presentation slides and another is
the presenter note file.

4. Open both files using Skim. In the presentation file, go to View ->
Presentation Options --> Synchronized Notes Document, and select the
presenter note pdf file.

5. Drag the presentation window to the projector screen and keep the
presenter note window in the laptop screen.

6. Start the presentation. The presenter note page will follow the
presentation page.

Typesetting Thai

Make Texshop aware of Thai encoding

add the following two lines
before \begin{document}

%!TEX TS-program = xelatex
%!TEX encoding = UTF-8 Unicode

From: http://poonlap.blogspot.com/2012/03/tex-mac-os-x.html

Thai hyphenation 

\usepackage{polyglossia}
\XeTeXlinebreaklocale "th_TH"
\XeTeXlinebreakskip = 0pt plus 1pt

Use different fonts for different languages

http://tex.stackexchange.com/questions/183424/xelatex-how-to-specify-a-different-font-family-for-latin-text-within-non-latin

The package ucharclasses does this. For short insertions of text in other alphabets hyphenation shouldn't be a concern; for longer parts, using the various features of polyglossia is recommended.
\documentclass{article}

\usepackage{polyglossia}
\usepackage{fontspec}
\setmainlanguage{thai}

\newfontfamily\thaifont{Thonburi}
\newfontfamily\englishfont{TeX Gyre Bonum} % just to get a distinctive font

\usepackage[Latin,Thai]{ucharclasses}

\setTransitionsForLatin
  {\begingroup\englishfont}
  {\endgroup}

\begin{document}

ดาวพุธมีหลุมอุกกาบาตจำนวนมากจนดูคล้ายดวงจันทร์ ภูมิลักษณ์ที่เด่นที่สุดบนดาวพุธ คือ
แอ่งแคลอริส (Caloris Basin) ดาวพุธมีหลุมอุกกาบาตจำนวนมากจนดูคล้ายดวงจันทร์
ภูมิลักษณ์ที่เด่นที่สุดบนดาวพุธ คือ แอ่งแคลอริส

\end{document}

enter image description here

List

Nesting enumerate

# Make it 1, 1.1, 1.2, 2, 2.1, 2.2
\renewcommand{\labelenumi}{\arabic{enumi}.} 
\renewcommand{\labelenumii}{\arabic{enumi}.\arabic{enumii}}

# Make it A, a/b/c, B, a/b/c
\renewcommand{\labelenumi}{\Alph{enumi}}
\renewcommand{\labelenumii}{\alph{enumii}}

Change the symbols in the list environment

Define the new environment as

\newcounter{bean}
\newenvironment{iletter}{\begin{list}{(\alph{bean})}{\usecounter{bean}\setlength{\parsep}{-1pt}}}{\end{list}}
\newenvironment{iLetter}{\begin{list}{\Alph{bean}.}{\usecounter{bean}\setlength{\parsep}{-1pt}}}{\end{list}}
\newenvironment{question}{\begin{enumerate}{}{\setlength{\leftmargin}{0pt}%
\setlength{\rightmargin}{\leftmargin}%
\setlength{\parsep}{-10pt}}}{\end{enumerate}}

Reduce spacing between successive items

\addtolength{\itemsep}{-0.1in}

Some useful variables are

    Page Layout
        \columnsep: gap between columns
        \topmargin: gap above header
        \topskip: between header and text
        \textheight: height of main text
        \textwidth: width of text
        \oddsidemargin: odd page left margin
        \evensidemargin : even page left margin
    Paragraphs
        \parindent: indentation of paragraphs
        \parskip: gap between paragraphs
    Floats (tables and figures)
        \floatsep: space left between floats.
        \textfloatsep: space between last top float or first bottom float and the text.
        \intextsep : space left on top and bottom of an in-text float.
        \dbltextfloatsep is \textfloatsep for 2 column output.
        \dblfloatsep is \floatsep for 2 column output.
        \abovecaptionskip: space above caption
        \belowcaptionskip: space below caption
    Maths
        \abovedisplayskip: space before maths
        \belowdisplayskip: space after maths
        \arraycolsep: gap between columns of an array
    Lists
        \topsep: space between first item and preceding paragraph.
        \partopsep: extra space added to \topsep when environment starts 
a new paragraph.
        \itemsep: space between successive items.

\vspace{-0.2in}
\begin{figure}[h]

List environment without spacing before/after paragraphs and between two list item,
\usepackage{enumitem}
\setlist{nolistsep}


To set space before equations, put these lines after \begin{document}

\setlength{\abovedisplayskip}{10.0pt plus 2.0pt minus 5.0pt}
\setlength{\belowdisplayskip}{10.0pt plus 2.0pt minus 5.0pt}
\setlength{\abovedisplayshortskip}{0.0pt plus 3.0pt}
\setlength{\belowdisplayshortskip}{6.0pt plus 3.0pt minus 3.0pt}
\setlength{\abovecaptionskip}{10.0pt plus 2.0pt minus 5.0pt}
\setlength{\belowcaptionskip}{10.0pt plus 2.0pt minus 5.0pt}

Reduce column spacing in tabular environments
\begin{table}[!h]\centering
\caption{}\label{}\small
\small\addtolength{\tabcolsep}{-2pt}

\begin{tabular}

Change fixed titles in contents, list of figures, chapter, etc.

\renewcommand*\listfigurename{List of figures}
\renewcommand{\appendixname}{ภาคผนวก}
\renewcommand{\bibname}{บรรณานุกรม}
\renewcommand{\chaptername}{บทที่}
\renewcommand*\contentsname{สารบัญ}
\renewcommand{\figurename}{รูปที่}
\renewcommand{\indexname}{ดัชนี}
\renewcommand{\tablename}{ตารางที่}
Also, \listfigurename, \listtablename

Add footnote to section title

\section[XXX]{XXX\footnote{Skipped this..}}

The text in [XXX] lets latex use appropriate text in the 
table of contents b/c a footnote is the table of contents
is not allowed. 

Compile part of file

\ifdefined\myflag
  Your code here
\else
  Another code
\fi

Then, compile with

xelatex or pdflatex -job-name=myfilexx "\def\myflag{}\input{myfile}"

\newif\ifsoln \solnfalse
\solntrue  %% Comment out if not compile the block..

\begin{document}

\ifsoln 
  Your code here
\fi

===========================

\usepackage{ifthen}
\newboolean{prob}
\setboolean{prob}{false} % boolvar=true or false

\begin{document}

\ifthenelse {\boolean{prob}}{Code if true}{Code if false}

============================

\usepackage{comment}

\includecomment{soln}
\excludecomment{soln}

\begin{soln}
  xxxx 
\end{soln}

Alternatively, 

xelatex '\AtBeginDocument{\includecomment{soln}}\input{syllabus}'

or create a makefile with the following contents

student:
     latex '\AtBeginDocument{\excludecomment{soln}}\input{syllabus}'
     
teacher:
     latex '\AtBeginDocument{\includecomment{soln}}\input{syllabus}'

Put the following at the first line will also set the output filename
%& -job-name=somename


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-- Using if then in the preamble
\
newcommand{\isitthree}[1]
{
  \ifnum#1=3
    number #1 is 3
  \else
    number #1 is not 3
  \fi
}


\newif\ifismonday
\newif\ifistuesday

\ismondayfalse
\istuesdaytrue

\ifismonday
  sucks!
\else
  \ifistuesday
    almost sucks.
  \else
    is \textbf{great}
  \fi
\fi

=======================================================

\usepackage{ifthen}
\newboolean{includethis}
\setboolean{includethis}{false}
\newcommand{\ifinclude}[1]{\ifthenelse{\boolean{includethis}}{#1}{}}


Then if you do
\ifinclude{%
\begin{frame}{Slide title}
 This slide will be hidden.
\end{frame}
}

the frame will be included only if you change the setting of 
the boolean variable includethis to 'true' in the preamble. 

======================================================
In your source file, write

\providecommand{\comment}[1]{\emph{#1}}% fallback definition

and then compile the LaTeX document ("myfile.tex") as


pdflatex (whatever options you need) "\newcommand\comment[1]{\textbf{#1}}\input{myfile}"

Mathematics

Change math font size

Add this to the preamble..

\makeatletter
\DeclareMathSizes{\@xipt}{10}{7}{5}  % For size 11pt text
\makeatother

The second argument = Math text size (in pt), 3rd = script size,
4th = scriptscript size

Matrix with partitions

\usepackage{pmat}
\begin{document}
\[
    \begin{pmat}[{.|}]
        a_{11} & a_{12} & b_{11} \cr
        a_{21} & a_{22} & b_{21} \cr\-
        c_{11} & c_{12} & d_{11} \cr
    \end{pmat}
\]

\end{document}

Large fraction

Use dfrac{}{} in amsmath package to create a large fraction font

Phasor operator

Use package steinmetz and define the new command

\newcommand{\jphase}[1]{\phase{#1l}}

and modify steinmetz.sty by changing the linethickness to .3pt

\put(0,0){\line(1,0){\ph@x}}\put(0,0){\line(1,2.5){\ph@dx}}


Right-aligned matrix

\setcounter{}{20}  % Increase max columns from 10 to 20
\makeatletter
\renewcommand*\env@matrix[1][c]{\hskip -\arraycolsep
  \let\@ifnextchar\new@ifnextchar
  \array{*\c@MaxMatrixCols #1}}
\makeatother

and use

\begin{bmatrix}[r]
\end{bmatrix}

or

%% Change the matrix alignment to the right
\makeatletter
\def\env@matrix{\hskip -\arraycolsep
\let\@ifnextchar\new@ifnextchar
\array{*\c@MaxMatrixCols r}}
\makeatother

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Increase # columns from the default 10 to 20. 
%% Make the bmatrix right-aligned with \begin{bmatrix}[r]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\setcounter{MaxMatrixCols}{20} 
\makeatletter
\renewcommand*\env@matrix[1][c]{\hskip -\arraycolsep
  \let\@ifnextchar\new@ifnextchar
  \array{*\c@MaxMatrixCols #1}}

\makeatother

Add box/frame

1. For non-paragraph mode, using mbox or framebox
2. For paragraph mode, use package boxedminipage
   or package empheq (\begin{empheq}),
   or package fancybox \begin{Bitemize}, \begin{Beqnarray}
3. For whole page, use package fancybox, with \fancypage

Table and Figure

Prevent latex from putting figure in the center of a last page

Try this in the preamble

\makeatletter
\setlength{\@fptop}{0pt}
\makeatother

Adjust 0pt if the space is too tight

Resize oversized table/figure to fit page

\resizebox{0.9\textwidth}{!}{
\begin{tabular}{llll}
\end{tabular}

}

Reduce space between columns


\begin{table}
\renewcommmand{\tabcolsep}{0.1cm} 

Draw a box around some part of texts

\fbox{
\begin{minipage}{0.95\textwidth}
\ul{Part I: Instructions}
\begin{itemize}
\item Indicate whether each statement is true or false. Mark $\times$
in Part I of the provided answer sheet.
\item A correct answer gets +1 point, an incorrect answer gets -0.5
point, no answer gets 0 point.
\end{itemize}%
\end{minipage}%

Monday, February 24, 2014

Numbering

Revert effect of \numberwithin

If  \numberwithin{equation}{section} is defined and ,
the equation environment is used with out first creating a section,
it will produce 0.x numbering.  To revert the effect of \numberwithin, use
the following:

\usepackage{chngcntr}

\counterwithout{equation}{section}

The command



\counterwithin{equation}{section}
will do the opposite.

This command applies to figure and table environments as well.


Make equation numbering changes with sections, chapters,


\numberwithin{equation}{section} %sets equation numbers <chapter>.<section>.<index>
\numberwithin{equation}{subsection} %sets equation numbers <chapter>.<section>.<subsection>.<index>
\numberwithin{equation}{subsubsection} %sets equation numbers <chapter>.<section>.<subsection>.<subsubsection>.<index>