CONTENTS

Format & Indention & Space

Space line

\hspace{1cm} is used to insert blank / horizental spaces in line, but void at the leftmost of a line.
If we want a force insertion of spaces at the beginning of a line, we can use hspace*{1cm}.

Back to CONTENTS


Equation in Latex

Equation

  • 如果是段内公式,前后要加上$\(\)
  • 如果公式独占一行且自动添加序号,则用\begin{equation}\end{equation}
  • 如果公式独占一行但不想编号,则用一对$$,或者\begin{equation*}\end{equation*}

Math Symbol

实数域、整数域等符号:

\usepackage{amssymb}  
$x \in \mathbb{R}^2$

效果:$x \in \mathbb{R}^2$ , $\mathbb{W}$

矩阵符号,不同于粗体:

\usepackage{bm}
$\bm{W}$

open brace for list

example:

\(\left\{ \begin{aligned} &x=R\cos\theta \\ &y = R\sin\theta \end{aligned} \right.\)

code:

$$
\left\{
\begin{aligned}
&x=R\cos\theta \\
&y = R\sin\theta
\end{aligned}
\right.
$$

align for = in equation

example:

\(\begin{aligned} x&=R\cos\theta \\ &= R\sin\theta \end{aligned}\)

code:

$$
\begin{aligned}
x&=R\cos\theta \\
&= R\sin\theta
\end{aligned}
$$

or equivelently

\begin{equation*}
\begin{split}
x&=R\cos\theta \\
&= R\sin\theta
\end{split}
\end{equation*}

Back to CONTENTS


figures

figure insertion

\begin{figure}[!ht]
\centering
\includegraphics[scale=0.7]{figures/Part1_accuracy_vs_k_0-120}
\caption{figname$i$}   % caption shown below figure
\label{accpart1}       % for reference
\end{figure}

two figures in one line

\begin{figure}[!ht]
\begin{minipage}[t]{0.5\linewidth}
\centering
\includegraphics[scale=0.55]{path/img1}
\caption{\footnotesize{figname}}   % caption shown below figure
\label{label1}                      % for reference
\end{minipage}
\begin{minipage}[t]{0.5\linewidth}
\centering
\includegraphics[scale=0.55]{path/img2.png}
\caption{figname2}                % caption shown below figure
\label{label2}                      % for reference
\end{minipage}
\end{figure}

Back to CONTENTS


tables

example:

code:

\begin{center}
\begin{tabular}{|r|c|c|}\hline
\multicolumn{1}{|c|}{predicted} & \multicolumn{2}{c|}{true label $y$}\\ \cline{2-3}
\multicolumn{1}{|c|}{label $\hat{y}$} & \ \ \ 0\ \ \ \   & 1 \\ \hline
0     & 0 & 10  \\ \hline
1     & 5 &  0  \\ \hline
\end{tabular}
\end{center}

In the codes above,
\hline add horizontal line in full width
\cline{2-3} add horizontal line - for column 2-3
\multicolumn{2}{c|} merge two corresponding columns together as one, then add a vertical line |
\\ start a new line in table

colors in table

example:

code:

\usepackage{colortbl}
\definecolor{mycyan}{cmyk}{.3,0,0,0}

\begin{tabular}
{|>{\columncolor{mycyan}\sf }c|c|c|c|c|@{}}
\hline
Byte Address & 1 & 348 & 756 & 9870 \\ \hline
Block Address & 0 & 2 & 5 & 77 \\ \hline
Block ID in cache & 0 & 2 & 5 & 13 \\ \hline
Hit/Miss & M & \textcolor{red}{H} & M & M \\ \hline
Replace? (Y/N) & N & \textcolor{red}{Y} & N & \textcolor{red}{Y}\\ \hline
\end{tabular}

In the codes above, add >{\columncolor{mycyan}\sf } before c in {|c|l|r|} part to color the first column, \sf is used to change font in this column.
Moreover, add \rowcolor{mycyan} before content of a row can color background of the whole row as the designed.

Back to CONTENTS


mini page for two column contents

setting width of minipage can put two horizontally in one line

\begin{minipage}{.4\textwidth}
$$
equation
$$
\end{minipage}  % This must go next to `\end{minipage}`

or              % words between two mini pages

\begin{minipage}{.4\textwidth}
\figure or something
\end{minipage}

Back to CONTENTS


Comment

加带颜色带边框文中注释: 侧边注释:\todo{Here's a comment in the margin!}
文中注释:as shown in the example on the right. You can also add inline comments: \todo[inline, color=green!40]{This is an inline comment.}

Back to CONTENTS


Pseudo code in Latex

algorithmicx is mostly used for pseudocode, if you want to include snippets of Python then maybe the package listings is a better choice (it comes with syntax highlighting for many languages, including Python). – Marijn Nov 11 ‘15 at 16:46

1、algorithm and algorithmic

用algorithm和algorithmic,带行号和分隔线
If {$i > \textit{stringlen}$} 中,If和then之间部分要加{};
斜体用$$,单词/变量名要紧凑就用\textit{},紧凑且不斜体就用\text{}
\State 用来另起一行并且固定格式
\IF {}, \ELSIF {}, \ELS, \ENDIF都大写省得麻烦

  • \IF {“condition”} “text” \ENDIF
  • \IF {“condition”} “text” \ELSE “text” \ENDIF
  • \IF {“condition”} “text” \ELSIF “text” \ELSIF “text” \ELSE “text” \ENDIF
  • \FOR {“condition”} “text” \ENDFOR
  • \FORALL {“condition”} “text” \ENDFOR
  • \WHILE {“condition”} “text” \ENDWHILE
  • \REPEAT “text” \UNTIL {“condition”}
  • \LOOP “text” \ENDLOOP

example:
psudocode_latex_1
code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother

\begin{document}
\begin{algorithm}               %算法的开始
\caption{Binary Tree Reconstruction}\label{euclid}	%算法的标题
\begin{algorithmic}[1]          %不知[1]是干嘛的?[1]表示显示行号
\Procedure{Node(preorder[1..n],inorder[1..n])}{}
\If {$n = 0$} \Return false
\ElsIf {$n = 1$} \Return $preorder[1]$
\Else
\State{$p \gets \text{Position of } \textit{preorder}[1] \text{ in } \textit{inorder}[1..n]$}
\If {$p > 1$}
\State $preorder[1].left  \gets \textit{Node}(preorder[2..p],inorder[1..p-1])$
\EndIf
\If {$p < n$}
\State $preorder[1].right  \gets \textit{Node}(preorder[p+1..n],inorder[p+1..n])$
\EndIf
\State \Return $preorder[1]$
\EndIf
\EndProcedure
\end{algorithmic}
\end{document} 

2、lstlisting

用lstlisting,保持所有原来字符和格式,没法用\;可以带上颜色,用前20行声明定义,
example:
psudocode_latex_2.png code:

\usepackage{listings}
\usepackage{color}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{frame=tb,
  language=Java,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=none,
  numberstyle=\tiny\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=3
}

\begin{lstlisting}
// to reconstruct an arbitrary binary tree with preorder \& inorder node sequences.
// assume the node sequence is identified with node, not only value, so no two nodes are identical.
Node(preorder[1..n],inorder[1..n])
    if n == 0 return NULL
    else if n == 1 return preorder[1]
    else p \leftarrow Position of preorder[1] in inorder
        preorder[1].left  = Node(preorder[2..p],inorder[1..p-1])
        preorder[1].right \leftarrow Node(preorder[p+1..n],inorder[p+1..n])
        return preorder[1]
public class Hello extends JApplet {
    public void paintComponent(Graphics g) {
        g.drawString("Hello, world!", 65, 95);
    }
}
\end{lstlisting}

Back to CONTENTS


Reference

  1. thebibliography生成文献列表,用\cite{}在文中引用,但是编号是由列表顺序决定而不是文中出现顺序决定。。。
  2. If you really need a bibliography sorted by citation order you have two choices:
    1. write the entries in thebibliography in citation order
    2. use a .bib file, \bibliographystyle{unsrt} and BibTeX. See the section below.

BIBTeX制作参考文献

BibTeX是一种格式和一个程序,用于协调LaTeX的参考文献处理。 BibTeX使用数据库的的方式来管理参考文献. BibTeX 文件的后缀名为.bib.

% example of .bib
@article{name1,
    author = {作者, 多个作者用 and 连接},
    title = {标题},
    journal = {期刊名},
    volume = {卷20},
    number = {页码},
    year = {年份},
    abstract = {摘要, 这个主要是引用的时候自己参考的, 这一行不是必须的}
    }

@book{name2,
    author ="作者",
    year="年份2008",
    title="书名",
    publisher ="出版社名称"
    }

说明:

  • 第一行@article告诉BibTeX这是一个文章类型的参考文献,还有其它格式, 例如 article, book, booklet, conference, inbook, incollection, inproceedings,manual, misc, mastersthesis, phdthesis, proceedings, techreport, unpublished 等等.
  • 接下来的”name1”,就是你在正文中应用这个条目的名称.

在LaTeX中使用BibTeX

为了在LaTeX中使用BibTeX数据库,你必须先做下面三件事情: 1) 设置参考文献的类型 (bibliography style). 标准的为 plain:

%如果文档类是article之类的, 用\renewcommand\refname{参考文献}
%如果文档类是book之类的, 用\renewcommand\bibname{参考文献}`
\renewcommand\bibname{参考文献}
\bibliographystyle{plain}

将上面的命令放在 LaTeX 文档的 \begin{document}后边. 其它的类型包括 - unsrt – 基本上跟plain类型一样,除了参考文献的条目的编号是按照引用的顺序,而不是按照作者的字母顺序. - alpha – 类似于 plain 类型,当参考文献的条目的编号基于作者名字和出版年份的顺序. - abbrv – 缩写格式 . 2) 标记引用 (Make citations). 当你在文档中想使用引用时, 插入 LaTeX 命令
\cite{引用文章名称}
“引用文章名称”就是前边定义@article后面的名称. 3) 告诉LaTeX生成参考文献列表,在 LaTeX 的结束前输入
\bibliography{bibfile}
这里bibfile 就是你的 BibTeX 数据库文件 bibfile.bib .

运行 BibTeX

分为下面四步:

  • 用LaTeX编译你的.tex文件 , 这时生成一个 .aux的文件, 这告诉 BibTeX 将使用那些应用;
  • 用BibTeX 编译.bib文件;
  • 再次用LaTeX编译你的.tex文件,这个时候在文档中已经包含了参考文献,但此时引用的编号可能不正确;
  • 最后用 LaTeX编译你的.tex文件,如果一切顺利的话, 这是所有东西都已正常了.

Back to CONTENTS


input and include

http://blog.csdn.net/lishoubox/article/details/6783726
处理较大的文件,一个有效的方法是把它分成几个部分,然后分别导入,

\input{filename1}
\input{filename2}

另一个常用的导入命令是

\include{filename1}  
\include{filename2}

区别在于:

  1. \include时总是开始新的一页,\input连续不分页。所以\include适合 book 类按 chapter 分割。
  2. \include命令,不能递归调用,\input命令可以。

http://www.eefocus.com/giema/blog/11-07/228074_8df8b.html

  1. include 和includeonly的联系
    今天学习latex发现include includeonly两个不知道有什么联系,经过一番baidu之后发现两个是有联系的。 includeonly放在导言区,用来指定正文区中include的哪一部分(或者全部)参与当前编译,对于大型文档的中间调试非常有用,不用修改正文区,只改includeonly的列表就可以实现分步调试了。
  2. include 和input的区别
    \input命令的缺点是,通过\input命令合并起来的文章(不管那部分) 每经过一次修改,所有的文件都要被重新读入和处理。若用\input命令仅加载那个特定的文件,那么所有的页码、章节、插图和公式等的自动编号都从1开始,这会使交叉引用产生混乱。更好的办法是采用\include命令。

http://blog.csdn.net/yelbosh/article/details/8631845
input命令可以改为include,区别在于,input可以放在导言区和正文区,包含的内容不另起一页;而include只能放在正文区,包含的内容另起一页。另外CJK中还有CJKinput和CJKinclude命令。

http://bbs.ctex.org/forum.php?mod=viewthread&tid=24600
\include反正要多处理一些东西,比如看看有没有\includeonly之类的,或者在\include执行时处理一个\cleardoublepage之类的

http://tex.stackexchange.com/questions/246/when-should-i-use-input-vs-include

\input{filename} imports the commands from filename into the target file; it’s equivalent to typing all the commands from filename right into the current file where the \input line is. \include{filename} essentially does a \clearpage before and after\input{filename}, together with some magic to switch to another .aux file, and omit the inclusion at all if you have an \includeonly without the filename in the argument. This is primarily useful when you have a big project on a slow computer; changing one of the include targets won’t force you to regenerate the outputs of all the rest \include gets you the speed bonus, but it also can’t be nested, can’t appear in the preamble, and forces page breaks around the included text.

Back to CONTENTS