【1.1】latex入门

为了达到自动化对文本标记的目的,花点时间琢磨一下latex,这样标记后的tex文件能自动生成.pdf文件。

(latex的安装略)

拓展阅读:

一份其实很短的 LaTeX 入门文档

TeX — Beauty and Fun

文档: https://www.tug.org/texlive/doc/texlive-en/texlive-en.html#x1-50001.3

一、安装:

wget -c http://mirror.ctan.org/systems/texlive/tlnet/install-tl-unx.tar.gz
tar -zxvf install-tl-unx.tar.gz
cd install-tl-20180201/
./install-tl

CentOS 8上安装PDFLatex

latex一直缺库,最后直接用的线上的latex工具:

可以通过以下步骤完成:

首先,确保您的系统已经安装了TeX Live。如果没有安装,可以按照以下步骤进行安装:

dnf install texlive-latex

接下来,安装PDFLatex。在终端中输入以下命令:

sudo yum install -y pdflatex

注意:为了方便使用,您可以将PDFLatex添加到系统PATH中。

一、创建一个文本test.tex,保存类型为utf-8

\documentclass{article}
 \begin{document}
 hello,word
 \end{document}

终端运行

xelatex test.tex

二、标题、作者、注释

\documentclass{article}
 \author{Sam}
 \title{Learn latex}

\begin{document}
 \maketitle
 hello,word %么么哒
 \end{document}

注释掉N行代码
\iffalse
.....
\fi

三、章节和段落

\documentclass{article}
\author{Sam}
\title{Learn latex}

\begin{document}
\maketitle
\section{Hello GenoWise} GenoWise is a great company
\subsection{Hello sam} Sam is here!
\paragraph{Honesty} is Sam's quality.
\subparagraph{Fortune} will be brought by Honesty.
\subsection{Hello Semal}
\paragraph{Smart} is Semal's quality.

\end{document}

内容连续空行

\vspace{1\baselineskip}

修改数字即可控制空行数

四、加目录

\documentclass{article}
 \author{Sam}
 \title{Learn latex}

\begin{document}
 \<span style="color: #ff0000;">tableofcontents</span> %加一个就行,不用每个section前都加
\clearpage %开始新的页
 \section{Hello GenoWise} GenoWise is a great company
 \subsection{Hello sam} Sam is here!
 \paragraph{Honesty} is Sam's quality.
 \subparagraph{Fortune} will be brought by Honesty.
 \subsection{Hello Semal}
 \paragraph{Smart} is Semal's quality.

\end{document}

xelatex **.tex  需要多运行几次,才可以

五、换行

\documentclass{article}
 \begin{document}
 Sam is
 a good boy.

Semal is

also

a good boy.

GenoWise is \\ a great\\
 company.
 \end{document}
 空行代表换行,\\代表打头换行。

六、表格

\documentclass{article}
 \begin{document}

\begin{tabular}{|c|c|}
 a & b \\
 c & d \\
 \end{tabular}

\begin{tabular}{|c|c|}
 \hline
 a & b \\
 \hline
 c & d \\
 \hline
 \end{tabular}

\begin{center}
 \begin{tabular}{|c|c|}
 \hline
 a & b \\
 \hline
 c & d \\
 \hline
 \end{tabular}
 \end{center}
 \end{document}

合并水平表格

\begin{tabular}{|c|c|c|c|}
 \hline
 Location: & \<span style="color: #ff0000;">multicolumn{3}{</span>|c|}{13:111111}\\
 \hline
 Allele1: & G & Frequency: & 0.111 \\
 \hline
 Allele2: & C & Frequency & 0.111\\
 \hline
 \end{tabular}

合并竖直表格

Latex里面的表格默认是没有边框的,如果想要让表格有边框的话需要使用命令进行设置。 Latex插入水平边框的命令是\hline,这会在出现这个命令的表格位置针对整个表格水平范围插入一个水平边框。如果想要在表格的部分内容上面插入水平边框的话可以使用命令\cline{i-j}, 其中i-j指边框跨越i到j这几个单元格。需要注意的是,如果你只需要在一个单元格上面插入边框,你也需要使用{i-i}的格式,i为需要插入的单元格,不能只写{i}

\begin{tabular}{|c|c|c|c|c|c|}
    \hline
        名字 & 序列 & 长度 & GC & Tm & Delata Tm \\
    \hline
        Probe1 & CATTGTACCTGCCACAGTAGATGCTC &\multicolumn{2}{|c|}{13:111111} & {\textcolor{red}{60}} &1 \\
    \hline
        Probe2 & CATTGTACCTGCCACAGTAGATGCTC & 20 & 57 & {\textcolor{red}{60}} &1 \\
    \hline
        Probe2 & CATTGTACCTGCCACAGTAGATGCTC & 20 & \multirow{2}{*}{57} & {\textcolor{red}{60}} &1 \\
    \cline{1-3}
    \cline{5-6}
        Probe2 & CATTGTACCTGCCACAGTAGATGCTC & 20 &  & {\textcolor{red}{60}} &1 \\
    \hline
\end{tabular}

注意:

\multicolumn{2}{|c|}{13:111111}

c左右的|,加或者不加,根据左右是否有|来决定

效果如下图

latex_table

表格内单元格内容强制换行

\newcommand{\tabincell}[2]{\begin{tabular}{@{}#1@{}}#2\end{tabular}}%放在导言区
然后使用 &\tabincell{c}{第一行内容\\第二行内容}& 就可以在表格中自动换行

第二种换行的方式

\documentclass{article}
\usepackage{array}
\newcolumntype{L}{>{\centering\arraybackslash}m{3cm}}

\begin{document}

\begin{table}
\begin{tabular}{|L|c|L|}\hline
one & two & three \\\hline
This is two line thing and centered & only one line&  \multicolumn{1}{m{3cm}|}{This is justified and may go to second line as well, neatly}\\\hline
  one & two & three \\\hline
  one & two & three \\\hline
\end{tabular}
\end{table}
\end{document}

latex_table

参考资料:http://tex.stackexchange.com/questions/54069/table-with-text-wrapping

七、加颜色

\documentclass{article}

\usepackage{color}
 \begin{document}

\begin{tabular}{|c|c|}
 a & b \\
 c & d \\
 \end{tabular}

\begin{tabular}{|c|c|}
 \hline
 a & b \\
 \hline
 c & d \\
 \hline
 \end{tabular}

\begin{center}
 \begin{tabular}{|c|c|}
 \hline
 a & b \\
 \hline
 {\color{red}{c}} & d \\
 \hline
 \end{tabular}
 \end{center}
 \end{document}

两点:

\usepackage{color} 调用颜色包
{\color{red}{c}} 对c加颜色

表格中加颜色

用\textcolor{red}{内容}
如果\color{red}{...}  容易导致内容跨行

加背景色

\colorbox{yellow}{yellow background}
 \colorbox[rgb]{1,0,0}{red background}
 \fcolorbox{blue}{yellow}{red background with a blue frame}

加下划线

\underline{aaaaaa}

八、定义长字符串的宽度

latex不能区分长字符串,出来的都是乱码。

\usepackage{seqsplit}
\newcommand\foo[2]{%
 \begin{minipage}{#1}
 \seqsplit{#2}
 \end{minipage}
 }
 \foo{\textwidth}
 {CCAAGTCGTGTGTTTACCTATATAACAAACTGCACATACATCCCTGAACCTAAAATAAAAGTTAAAATATTTTTAAAAAGAGAGAAACATCAATCCTTAAtATTAACTAAATAGGAAAATACCAGCTTCATAGACAAAGGTTCTCTTTGACTCACCTGCAATAAGTTG{\colorbox{yellow}{CCTTATTAAcgGTATC}}TTCAGAAGAATCAGAT[C/-]CTAAAAAATTTCCCCCCAAAAAATAAATCAATAAAAGTTTTCTTAATTAAAAGGGTTaAAAAAATGTACTTGTTGAAAAACAGATATTCAACTAGAAATATTTACTGAGCATCTACTGTGGCAGGTACAATGCTAGTTAAGCTAGTATGTAATACAGAGAAGTGGTTAAAGGCATGGGCTTCGCCAGGCACAGTGGCTCT}

报错:

! Undefined control sequence
 \SQSPL@end

解决办法: 最后一个}要跟字符串在一行,否则就会报错,什么原因,我也没搞清楚。

如果需要在里面给字体加上红色背景或者红色字体,或者下划线,需要

{\underline{AACTGCA}}CATACA{{\color{red}{60}}}TCCC{\colorbox{yellow}{TGAAC}}CT

特别注意color需要外包两层颜色。

九、中文编辑问题

\usepackage{xeCJK, fontspec}
 % set font
 \setmainfont{Arial}
 \setCJKmainfont{Microsoft YaHei}

折腾了半天,最后发现我本地上传的文件是GBK的编辑方式,而服务器默认的是utf-8

dos2unix latex_test.tex

将GBK文档转码成utf-8

Latex 换行顶格、不缩进,使用的命令为: \noindent

十、加空格

如果需要多个空格, 可以使用多个

\hspace{ 长度 } , 例如 \hspace{1cm}

其中的长度单位cm 可以换成 mm, em, in, pt等.

如果\hspace命令在一行的开始则需要改用\hspace*{1cm}来产生空格. 如果要产生垂直方向的空白, 可使用命令

\vspace{ 长度 } 和 \vspace*{ 长度 } , 使用方法同\hspace.

十一、开启新页

\clearpage

十二、加链接跳转

需要加载包

\usepackage{hyperref}

需要跳转到的位置加上label

\label{rs6754640}

需要进行跳转的加上ref

\ref{rs6754640}

十三、插入图片

\usepackage{graphicx}
\includegraphics[width=\linewidth,height=2pt]{图片文件}

出入的图片加入caption

\usepackage{graphicx}

\begin{figure}[htbp]
\centering
\includegraphics[width=\linewidth,height=2pt]{图片文件}\caption{这是图片标题}
\end{figure}

如果在tcolorbox中插入图片

\usepackage{caption}
\usepackage{graphicx}

\begin{minipage}{\linewidth}
\includegraphics[width=\linewidth,height=200pt]{Mean_amplicons_depth_cv.pdf}
\captionof{figure}{Mean-depth and depth-CV of amplicons}
\end{minipage}

参考资料:http://blog.sina.com.cn/s/blog_976290d401014bv1.html

十四、清除一页中的页码标记

\thispagestyle{empty}

十五、调整margin

\usepackage[a4paper]{geometry}
全局的margin
\geometry{
    left=0.8cm,
    right=0.8cm,
    top=2.5cm,
    bottom=2.63cm,
    footskip=1.48cm,
    headsep=.5cm,
    headheight=1.5cm
}
单页的margin
\newgeometry{left=0.3cm,right = 0.3cm,top=0.1cm,bottom=0.1cm}
....
\restoregeometry

四、报错

报错4.1 LaTeX Error: File `standalone.cls' not found

这个其实没解决,缺的库太多了。记录一下,因为觉得这些方法,应该可以

方法1:因为缺ydocstrip库,放弃了

wget -c https://mirrors.ctan.org/macros/latex/contrib/standalone.zip

[root@4c2bdfc52747 standalone]# pdflatex standalone.ins

This is pdfTeX, Version 3.14159265-2.6-1.40.19 (TeX Live 2018) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./standalone.ins
LaTeX2e <2017-04-15>
Babel <3.17> and hyphenation patterns for 3 language(s) loaded.
! I can't find file `ydocstrip.tex'.
l.1 \input ydocstrip.tex

(Press Enter to retry, or Control-D to exit)
Please type another input file name:
! I can't find file `ydocstrip.tex'.
l.1 \input ydocstrip.tex

(Press Enter to retry, or Control-D to exit)
Please type another input file name:
! I can't find file `ydocstrip.tex'.
l.1 \input ydocstrip.tex

(Press Enter to retry, or Control-D to exit)
Please type another input file name:

方法二:因为缺adjustbox,放弃了

[root@4c2bdfc52747 standalone]# wget -c https://www.rpmfind.net/linux/epel/9/Everything/x86_64/Packages/t/texlive-standalone-20200406-33.el9.noarch.rpm
--2023-10-08 08:49:42--  https://www.rpmfind.net/linux/epel/9/Everything/x86_64/Packages/t/texlive-standalone-20200406-33.el9.noarch.rpm
Resolving www.rpmfind.net (www.rpmfind.net)... 195.220.108.108
Connecting to www.rpmfind.net (www.rpmfind.net)|195.220.108.108|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 295787 (289K) [application/x-rpm]
Saving to: ‘texlive-standalone-20200406-33.el9.noarch.rpm’

texlive-standalone-202004 100%[=====================================>] 288.85K   170KB/s    in 1.7s

2023-10-08 08:49:45 (170 KB/s) - ‘texlive-standalone-20200406-33.el9.noarch.rpm’ saved [295787/295787]

[root@4c2bdfc52747 standalone]# rpm -i texlive-standalone-20200406-33.el9.noarch.rpm
warning: texlive-standalone-20200406-33.el9.noarch.rpm: Header V4 RSA/SHA256 Signature, key ID 3228467c: NOKEY
error: Failed dependencies:
        tex(adjustbox.sty) is needed by texlive-standalone-9:20200406-33.el9.noarch
        tex(filemod-expmin.sty) is needed by texlive-standalone-9:20200406-33.el9.noarch
        tex(gincltex.sty) is needed by texlive-standalone-9:20200406-33.el9.noarch
        tex(trimclip.sty) is needed by texlive-standalone-9:20200406-33.el9.noarch

方法3:

[root@4c2bdfc52747 LinearPartition]# tlmgr install standalone --ignore-warning
tlmgr: Remote repository is newer than local (2017 < 2018)
Cross release updates are only supported with
  update-tlmgr-latest(.sh/.exe) --update
Please see https://tug.org/texlive/upgrade.html for details.

[root@4c2bdfc52747 LinearPartition]# tex --version
TeX 3.14159265 (TeX Live 2018)
kpathsea version 6.3.0
Copyright 2018 D.E. Knuth.
There is NO warranty.  Redistribution of this software is
covered by the terms of both the TeX copyright and
the Lesser GNU General Public License.
For more information about these matters, see the file
named COPYING and the TeX source.
Primary author of TeX: D.E. Knuth.

[root@4c2bdfc52747 LinearPartition]# tlmgr --version
tlmgr revision 45286 (2017-09-13 03:55:27 +0200)
tlmgr using installation: /usr/share/texlive
TeX Live (http://tug.org/texlive) version 2017


[root@4c2bdfc52747 LinearPartition]# tlmgr repository list
List of repositories (with tags if set):
        /usr/share/texlive (main)

[root@4c2bdfc52747 LinearPartition]# tlmgr option repository ftp://tug.org/historic/systems/texlive/2017/tlnet-final

tlmgr: setting default package repository to ftp://tug.org/historic/systems/texlive/2017/tlnet-final
tlmgr: Cannot save 00texlive.installation to /usr/share/texlive/tlpkg/tlpobj/00texlive.installation.tlpobj
tlmgr: action option returned an error; continuing.
tlmgr: An error has occurred. See above messages. Exiting.

[root@4c2bdfc52747 /]# mkdir -p /usr/share/texlive/tlpkg/tlpobj


[root@4c2bdfc52747 /]# tlmgr option repository ftp://tug.org/historic/systems/texlive/2017/tlnet-final
tlmgr: setting default package repository to ftp://tug.org/historic/systems/texlive/2017/tlnet-final

[root@4c2bdfc52747 /]# tlmgr repository list
List of repositories (with tags if set):
        ftp://tug.org/historic/systems/texlive/2017/tlnet-final (main)

[root@4c2bdfc52747 LinearPartition]# tlmgr install standalone
*** WARNING ***: Performing this action will likely destroy the RHEL TeXLive install on your system.
*** WARNING ***: This is almost NEVER what you want to do.
*** WARNING ***: Try using dnf install/update instead.
*** WARNING ***: If performing this action is really what you want to do, pass the "ignore-warning" option.
*** WARNING ***: But please do not file any bugs with the OS Vendor.

[root@4c2bdfc52747 LinearPartition]# tlmgr install standalone --ignore-warning                          
tlmgr: package repository ftp://tug.org/historic/systems/texlive/2017/tlnet-final (not verified: pubkey missing)
tlmgr install: package already present: standalone

参考资料

药企,独角兽,苏州。团队长期招人,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn