1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 頁 1/6 % Header % \documentclass[11pt]{article} \usepackage{epsfig, color} \usepackage[svgnames]{xcolor} \usepackage{moreverb} % source code listing \usepackage{listings} % R source code \usepackage{fancybox} \usepackage{hyperref} % links % 中文設定 % \usepackage{xltxtra, fontspec, xunicode} % 設定字體 \usepackage[slantfont, boldfont]{xecjk} % 允许斜体和粗体 % 讓中英文字體分開設置 \setcjkmainfont{ 微軟正黑體 } % 可以改用 標楷體 細明體 \XeTeXlinebreaklocale "zh" % 中文才能自動換行 \XeTeXlinebreakskip = 0pt plus 1pt % 中文才能自動換行 % R 程式碼列印設定 % \lstset{language=r, basicstyle=\ttfamily, stringstyle=\color{red}, otherkeywords={0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, morekeywords={true, FALSE}, keywordstyle=\color{blue}, commentstyle=\tt\color{darkgreen} } % Page Format % \setlength{\textheight}{23cm} \setlength{\textwidth}{16cm} \setlength{\topmargin}{-1in} \setlength{\oddsidemargin}{0in} \setlength{\evensidemargin}{0in} % renewcommand % \newcommand{\mt}{\multicolumn} \newcommand{\blue}[1]{\textcolor{blue}{#1}} \renewcommand{\contentsname}{ 目錄 } % Contents \renewcommand{\figurename}{ 圖 } % Figure \renewcommand{\tablename}{ 表 } % Table \renewcommand{\refname}{ 參考文獻 } % References \renewcommand{\listfigurename}{ 圖目錄 } % List of Figures \renewcommand{\listtablename}{ 表目錄 } % List of Tables
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 頁 2/6 \renewcommand{\baselinestretch}{1.2} % Title % \begin{document} \title{\latex \ 教學範例文件 } \author{ 吳漢銘 \footnote{e-mail: {\it hmwu@gm.ntpu.edu.tw}} \\ {\it 國立臺北大學統計學系 }} \date{\today} \maketitle \tableofcontents \listoffigures \listoftables \clearpage % Section: Introduction % \section{ 簡介 }\label{set:intro} Hello {\Large everyone}! This {\bf is} a \LaTeX\footnote{\url{https://www.latex-project.org}} \ \textcolor{red}{example} {\tt document}. 這是一份 \LaTeX {\bf 教學文件 }, 希望大家學習愉快 \begin{itemize} \item 要找一本好的工具書來參考, 例如 Shultis (1994) \cite{shultis94} \item 安裝軟體為 MikteX (\url{https://miktex.org} ) 和 TeXStudio (\url{http://www.texstudio.org} ) \item 文件副檔名為 {\tt.tex} 有分中文文件及英文文件 \item 開啟範例檔案測試 : {\tt C:\textbackslash Program Files (x86)\textbackslash TeXstudio\textbackslash templates\textbackslash template\_article.tex} Build \ & View (F5) \item 編譯過程 : PDFLaTeX( 英文文件 ),XeLaTeX( 中英文文件 ) \item 編譯及預覽 ( 英文 ): Tools $\Rightarrow$ Build \ & View 或 Tools $\Rightarrow$ Commands $\Rightarrow$ PDFLaTeX \item 編譯及預覽 ( 中英文 )(F6): Tools $\Rightarrow$ Commands $\Rightarrow$ XeLaTeX \item 使用 \verb \label{maker} 和 \verb \ref{maker} 例如第 \ref{set:math} 節中的式子 (\ref{def:derivative} ) 是微分的定義 圖 \ref{fig:iris} 是一個散佈圖, 它在第 \pageref{fig:iris} 頁 ( 要編譯兩次以上, 才可更新目錄或編號 ) \item 自行 google 這些巨集套件的用法 : {\tt moreverb} {\tt listings} {\tt fancybox} {\tt hyperref} \item \ % 是註解符號 用 \verb \iffalse... \fi 註解一個段落 文字 a 數式 $a$ 程式碼 {\tt a} \item 線上 \LaTeX: \begin{itemize} \item \blue{\url{https://www.tutorialspoint.com/online\_latex\_editor.php}}
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 頁 3/6 \item \blue{\url{https://www.overleaf.com}} \item \blue{\url{https://www.latex4technics.com}} \end{itemize} \item 符號表 \begin{itemize} \item 選按 \href{http://tug.ctan.org/info/symbols/comprehensive/symbols-a4.pdf} { The Comprehensive LaTeX Symbol List } 即可連結 \item \blue{https://oeis.org/wiki/list\_of\_latex\_mathematical\_symbols} \end{itemize} \end{itemize} % Section: Equations % \section{ 數學式 }\label{set:math} \subsection{ 文中數式 {\tt \$... \$}} \begin{enumerate} \item 大家最熟悉的ㄟ K 死霸 : $\bar{x}=\sum_{i=1}^n x_i/n$ (\verb $\bar{x}=\sum_{i=1}^n x_i/n$ ) \item 要計算 $\int_0^{\pi/2} \sin^2 x\ dx$ 很簡單 (\verb $\int_0^{\pi/2} \sin^2 x\ dx$ ) \end{enumerate} \subsection{ 單獨數式 {\tt \textbackslash[... \textbackslash]}} \begin{enumerate} \item \verb \[... \], 單一數式無標號 號稱最美的數學公式 : \\ (\verb \[ e^{i\pi} + 1 = 0. \qquad (\mbox{ 歐拉恆等式 })\] ) \[ e^{i\pi} + 1 = 0. \qquad (\mbox{ 歐拉恆等式 })\] \item \verb \begin{equation}... \end{equation}, 單一數式有標號 \\ The derivative of a function $f$ at a point $x_0$, denoted $f'(x_0)$, is \begin{equation} f'(x_0) = \lim_{h \rightarrow 0} \frac{f(x_0+h)-f(x_0)}{h} \label{def:derivative} \end{equation} provides this limit exists. \item \verb \begin{eqnarray}... &... &... \end{eqnarray}, 多數式有標號 \\ \begin{eqnarray} \lim_{x\rightarrow \infty} \left( 5 + \frac{1}{x} \right) &=& \lim_{x\rightarrow \infty} 5 + \lim_{x\rightarrow \infty} \frac{1}{x}\\ &=& 5 + 0 \\ &=& 5 \end{eqnarray} \end{enumerate} \label{ex:answer} % Section: Figure %
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 頁 4/6 \section{ 圖形 }\label{set:figure} 記得圖檔和 tex 檔要放在同一目錄, 可直接利用 \verb \ includegraphic 引入圖形 圖形分兩種格式類型 : 壓縮格式圖形 ( 例如 : JPEG/PNG 檔, 如以下 ) 和向量圖形 ( 例如 : EPS/PDF 檔, 如圖 \ref{fig:iris} ) \begin{center} \includegraphics[scale=0.3]{r_logo.svg.png} \includegraphics[scale=0.6]{rstudio.jpg} \end{center} % 全文照印環境 \begin{verbatim} \begin{center} \includegraphics[scale=0.3]{r_logo.svg.png} \includegraphics[scale=0.6]{rstudio.jpg} \end{center} \end{verbatim} \begin{figure}[htbp] \centering \includegraphics[scale=0.6]{iris.pdf} \caption{ 圖形置於 figure 環境中, 自動編號 }\label{fig:iris} \end{figure} % Section: Table % \section{ 表格 }\label{set:table} 這裡很多範例可以參考 : \url{https://en.wikibooks.org/wiki/latex/tables} \subsection*{\underline{ 簡單表格 }} 以 \verb \begin{tabular}... \end{tabular} 製作表格, 可用 \verb \begin{center}... \end{center} 置中 \\ \begin{minipage}[c]{0.5\linewidth} \begin{tabular}{l c r} \hline Software & Price & Good \\ \hline MikTeX & 56 & Yes \\ TeXstudio & 8 & Yes \\ \hline \end{tabular} \end{minipage} \begin{minipage}[c]{0.5\linewidth} \begin{verbatim} \begin{tabular}{l c r} \hline Software & Price & Good \\ \hline MikTeX & 56 & Yes \\ TeXstudio & 8 \end{tabular} \end{verbatim} & Yes \\ \hline
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 頁 5/6 \end{minipage} \subsection*{\underline{ 置於 {\tt table} 環境裡的表格 }} \verb \begin{table}\begin{tabular}... \end{tabular} \end{table} \begin{table}[htb] \caption{characteristics of the datasets. All datasets contain no missing observations.} \label{data} \centerline{} \begin{center} \begin{tabular}{ cc rrrrr } \hline & & \mt{5}{c }{Number of attributes} \\ \cline{3-7} Data& No. of & Score &\mt{3}{c}{categorical}& Total\\ \cline{4-6} Set & Cases& & A & B & C & \\ \hline {\tt bcw} & 683 & 9 & $\times$ & $\vee$ & $\pm$ & 9 \\ {\tt cmc} & 1473 & 2 & 3 & \"{o} & 4 & 9 \\ {\tt wav} & 600 & 21& $\clubsuit$ & $\alpha$ & $\beta$ & 21 \\ \hline \end{tabular} \end{center} \end{table} % Section: Reference % \addcontentsline{toc}{section}{ 參考文獻 } \begin{thebibliography}{99} \bibitem{shultis94} Shultis, J. K., 1994, \LaTeX Notes: Practical Tips for Preparing Technical Documents, Englewood Cliffs, N.J. : PTR Prentice Hall. \bibitem{goossens94} Goossens, M., Mittelbach, F., and Samarin, A., 1994, The \LaTeX \ Companion, Reading, Mass.: Addison. \end{thebibliography} % Appendix % \addcontentsline{toc}{section}{ 附錄 } \appendix \section{ 程式碼 ( 一般 / 加框 )} 程式碼不可用細明體, 不可用 Time New Roman 字體 \\ \begin{sbox} \begin{minipage}{12cm} \begin{verbatim} rmvn.svd <- function(n, mu, Sigma){ d <- length(mu) S <- svd(sigma) R <- S$u %*% diag(sqrt(s$d)) %*% t(s$v) #sqrt Sigma
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 Z <- matrix(rnorm(n*d), nrow=n, ncol=d) X <- Z %*% R + matrix(mu, n, d, byrow=true) X } \end{verbatim} \end{minipage} \end{sbox} \fbox{\thesbox} \section{ 程式碼 ( 編行號 )} \begin{listing}[1]{1} x <- loc.mix(1000, 0.5, rep(0, 4), 2:5, Sigma=diag(4)) r <- range(x)*1.2 par(mfrow=c(2, 2)) for(i in 1:4){ hist(x[,i], xlim=r, ylim=c(0, 0.3), freq=false, main="") } \end{listing} \section{ 程式碼 ( 語法突顯 )} \begin{lstlisting} > dice <- sample(1:6, 10, replace=true) # 丟一骰子 10 次 > dice [1] 4 3 1 2 6 6 3 5 4 3 > mean(dice) # average [1] 3.7 \end{lstlisting} 註 : 你也可以直接匯入一個 {\sf R} 程式碼檔 : \verb \lstinputlisting[language=r]{myrcode.r} \end{document} % 文件結束 document 後面的文字就不會編譯了 頁 6/6