使用 LaTeX 和 TeXstudio 排版文档
LaTeX
![](/data/attachment/album/202102/20/094530itk2dim7yktyhh9y.jpg)


LaTeX 是一个服务于高质量排版的[文档准备系统](http://www.latex-project.org/about/)。通常用于大量的技术和科学文档的排版。不过，你也可以使用 LaTex 排版各种形式的文档。教师可以编辑他们的考试和教学大纲，学生可以展示他们的论文和报告。这篇文章让你尝试使用 TeXstudio。TeXstudio 是一个便于编辑 LaTeX 文档的软件。


### 启动 TeXstudio


如果你使用的是 Fedora Workstation，请启动软件管理，然后输入 TeXstudio 以搜索该应用程序。然后选择安装并添加 TeXstudio 到你的系统。你可以从软件管理中启动这个程序，或者像以往一样在概览中启动这个软件。


或者，如果你使用终端，请输入 `texstudio`。如果未安装该软件包，系统将提示你安装它。键入 `y` 开始安装。



```
$ texstudio
bash: texstudio: command not found...
Install package 'texstudio' to provide command 'texstudio'? [N/y] y

```

### 你的第一份文档


LaTeX 命令通常以反斜杠 `\` 开头，命令参数用大括号 `{}` 括起来。首先声明 `documentclass` 的类型。这个例子向你展示了该文档的类是一篇文章。


然后，在声明 `documentclass` 之后，用 `begin` 和 `end` 标记文档的开始和结束。在这些命令之间，写一段类似以下的内容：



```
\documentclass{article}
\begin{document}
The Fedora Project is a project sponsored by Red Hat primarily to co-ordinate the development of the Linux-based Fedora operating system, operating with the vision that the project "creates a world where free culture is welcoming and widespread, collaboration is commonplace, and people control their content and devices". The Fedora Project was founded on 22 September 2003 when Red Hat decided to split Red Hat Linux into Red Hat Enterprise Linux (RHEL) and a community-based operating system, Fedora.
\end{document}

```

![](/data/attachment/album/202102/20/094530pmsifrnmssjsigzz.png)


### 使用间距


要创建段落分隔符，请在文本之间保留一个或多个换行符。下面是一个包含四个段落的示例：


![](/data/attachment/album/202102/20/094531g3hfhbpyzg6ggybb.png)


从该示例中可以看出，多个换行符不会在段落之间创建额外的空格。但是，如果你确实需要留出额外的空间，请使用 `hspace` 和 `vspace` 命令。这两个命令分别添加水平和垂直空间。下面是一些示例代码，显示了段落周围的附加间距：



```
\documentclass{article}
\begin{document}

\hspace{2.5cm} The four essential freedoms

\vspace{0.6cm} 
A program is free software if the program's users have the 4 essential freedoms:

The freedom to run the program as you wish, for any purpose (freedom 0).\vspace{0.2cm} 
The freedom to study how the program works, and change it so it does your computing as you wish (freedom 1). Access to the source code is a precondition for this.\vspace{0.2cm}

The freedom to redistribute copies so you can help your neighbour (freedom 2).\vspace{0.2cm}

The freedom to distribute copies of your modified versions to others (freedom 3). By doing this you can give the whole community a chance to benefit from your changes. Access to the source code is a precondition for this.

\end{document}

```

如果需要，你也可以使用 `noindent` 命令来避免缩进。这里是上面 LaTeX 源码的结果：


![](/data/attachment/album/202102/20/094533wbp84btnha08st60.png)


### 使用列表和格式


如果把自由软件的四大基本自由列为一个清单，这个例子看起来会更好。通过在列表的开头使用`\begin{itemize}`，在末尾使用 `\end{itemize}` 来设置列表结构。在每个项目前面加上 `\item` 命令。


额外的格式也有助于使文本更具可读性。用于格式化的有用命令包括粗体、斜体、下划线、超大、大、小和 textsc 以帮助强调文本：



```
\documentclass{article}
\begin{document}

\hspace{2cm} {\huge The four essential freedoms}

\vspace{0.6cm} 
\noindent {\large A program is free software if the program's users have the 4 essential freedoms}:
\begin{itemize}
\item \vspace{0.2cm} 
\noindent \textbf{The freedom to run} the program as you wish, for any purpose \textit{(freedom 0)}. \vspace{0.2cm} 
\item \noindent \textbf{The freedom to study} how the program works, and change it so it does your computing as you wish \textit{(freedom 1)}. Access to the source code is a precondition for this.\vspace{0.2cm}

\item \noindent \textbf{The freedom to redistribute} copies so you can help your neighbour \textit{(freedom 2)}.\vspace{0.2cm}

\item \noindent \textbf{The freedom to distribute copies of your modified versions} to others \textit{(freedom 3)}. \tiny{By doing this you can give the whole community a chance to benefit from your changes.\underline{\textsc{ Access to the source code is a precondition for this.}}}
\end{itemize}
\end{document}

```

![](/data/attachment/album/202102/20/094534d4t0s5q504tkj4i6.png)


### 添加列、图像和链接


列、图像和链接有助于为文本添加更多信息。LaTeX 包含一些高级功能的函数作为宏包。`\usepackage` 命令加载宏包以便你可以使用这些功能。


例如，要使用图像，可以使用命令 `\usepackage{graphicx}`。或者，要设置列和链接，请分别使用 `\usepackage{multicol}` 和 `\usepackage{hyperref}`。


`\includegraphics` 命令将图像内联放置在文档中。（为简单起见，请将图形文件包含在与 LaTeX 源文件相同的目录中。）


下面是一个使用所有这些概念的示例。它还使用下载的两个 PNG 图片。试试你自己的图片，看看它们是如何工作的。



```
\documentclass{article} 
\usepackage{graphicx}
\usepackage{multicol}
\usepackage{hyperref}
\begin{document} 
 \textbf{GNU}
 \vspace{1cm}

 GNU is a recursive acronym for "GNU's Not Unix!", chosen because GNU's design is Unix-like, but differs from Unix by being free software and containing no Unix code.

 Richard Stallman, the founder of the project, views GNU as a "technical means to a social end". Stallman has written about "the social aspects of software and how Free Software can create community and social justice." in his "Free Society" book.
 \vspace{1cm}

 \textbf{Some Projects}

 \begin{multicols}{2}
 Fedora
 \url{https://getfedora.org}
 \includegraphics[width=1cm]{fedora.png}

 GNOME
 \url{https://getfedora.org}
 \includegraphics[width=1cm]{gnome.png}
 \end{multicols} 

\end{document}

```

![](/data/attachment/album/202102/20/094535j3bhb9s3js3vo3b3.png)


这里的功能只触及 LaTeX 功能的表面。你可以在该项目的[帮助和文档站点](https://www.latex-project.org/help/)了解更多关于它们的信息。




---


via: <https://fedoramagazine.org/typeset-latex-texstudio-fedora/>


作者：[Julita Inca Chiroque](https://fedoramagazine.org/author/yulytas/) 选题：[Chao-zhi](https://github.com/Chao-zhi) 译者：[Chao-zhi](https://github.com/Chao-zhi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
LaTeX is [a document preparation system](http://www.latex-project.org/about/) for high-quality typesetting. It’s often used for larger technical or scientific documents. However, you can use LaTeX for almost any form of publishing. Teachers can edit their exams and syllabi, and students can present their thesis and reports for classes. This article gets you started with the TeXstudio app. TeXstudio makes it easy to edit LaTeX documents.


**Launching TeXstudio**

If you’re using Fedora Workstation, launch *Software**, *and type **TeXstudio** to search for the app. Then select *Install* to add TeXstudio to your system. You can also launch the app from *Software*, or go to the shell overview as usual.

Alternately, if you use a terminal, type **texstudio**. If the package isn’t installed, the system prompts you to install it. Type *y* to start the installation.

$texstudiobash: texstudio: command not found... Install package 'texstudio' to provide command 'texstudio'? [N/y] y

**Your first document**

LaTeX commands typically start with a backslash (), and command parameters are enclosed in curly braces { }. Start by declaring the type of the *documentclass*. This example shows you the document class is an *article*.

Then, once you declare the *documentclass*, mark the beginning and the end of the document with *begin* and *end*. In between these commands, write a paragraph similar to the following:

\documentclass{article} \begin{document} The Fedora Project is a project sponsored by Red Hat primarily to co-ordinate the development of the Linux-based Fedora operating system, operating with the vision that the project "creates a world where free culture is welcoming and widespread, collaboration is commonplace, and people control their content and devices". The Fedora Project was founded on 22 September 2003 when Red Hat decided to split Red Hat Linux into Red Hat Enterprise Linux (RHEL) and a community-based operating system, Fedora. \end{document}

LaTeX is like source code compiled into an app you can run. The LaTeX source is processed so you can view the output document. To run (or process) the project, click on the green play icon. To display a preview, click on the magnifying-glass icon.

### Working with spacing

To create a paragraph break, leave one or more blank spaces between text. Here’s an example with four paragraphs:

You can see from the example that more than one line break doesn’t create additional blank space between paragraphs. However, if you do need to leave additional space, use the commands* hspace* and

*vspace.*These add horizontal and vertical space, respectively. Here is some example code that shows additional spacing around paragraphs:

\documentclass{article} \begin{document} \hspace{2.5cm} The four essential freedoms \vspace{0.6cm} A program is free software if the program's users have the 4 essential freedoms: The freedom to run the program as you wish, for any purpose (freedom 0).\vspace{0.2cm} The freedom to study how the program works, and change it so it does your computing as you wish (freedom 1). Access to the source code is a precondition for this.\vspace{0.2cm} The freedom to redistribute copies so you can help your neighbour (freedom 2).\vspace{0.2cm} The freedom to distribute copies of your modified versions to others (freedom 3). By doing this you can give the whole community a chance to benefit from your changes. Access to the source code is a precondition for this. \end{document}

If needed, you can also use the *noindent* command to avoid indentation. Here is the result of the LaTeX source above:

### Using Lists and Formats

This example would look better if it presented the four essential freedoms of free software as a list. Set the list structure by using *\begin{itemize}* at the beginning of the list, and *\end{itemize} *at the end. Precede each item with the command *\item.*

Additional format also helps make the text more readable. Useful commands for formatting include *bold, italic, underline, huge, large, tiny *and *textsc *to help emphasize text:

\documentclass{article} \begin{document} \hspace{2cm} {\huge The four essential freedoms} \vspace{0.6cm} \noindent {\large A program is free software if the program's users have the 4 essential freedoms}: \begin{itemize} \item \vspace{0.2cm} \noindent \textbf{The freedom to run} the program as you wish, for any purpose \textit{(freedom 0)}. \vspace{0.2cm} \item \noindent \textbf{The freedom to study} how the program works, and change it so it does your computing as you wish \textit{(freedom 1)}. Access to the source code is a precondition for this.\vspace{0.2cm} \item \noindent \textbf{The freedom to redistribute} copies so you can help your neighbour \textit{(freedom 2)}.\vspace{0.2cm} \item \noindent \textbf{The freedom to distribute copies of your modified versions} to others \textit{(freedom 3)}. \tiny{By doing this you can give the whole community a chance to benefit from your changes.\underline{\textsc{ Access to the source code is a precondition for this.}}} \end{itemize} \end{document}

### Adding columns, images and links

Columns, images and links help add further information to your text. LaTeX includes functions for some advanced features as packages. The *\usepackage* command loads the package so you can make use of these features.

For example, to make an image visible, you might use the command *\usepackage{graphicx}*. Or, to set up columns and links, use *\usepackage{multicol}* and *\usepackage{hyperref}*, respectively.

The *\includegraphics* command places an image inline in your document. (For simplicity, include the graphics file in the same directory as your LaTeX source file.)

Here’s an example that uses all these concepts. It also uses two PNG graphics files that were downloaded. Try your own graphics to see how they work.

\documentclass{article} \usepackage{graphicx} \usepackage{multicol} \usepackage{hyperref} \begin{document} \textbf{GNU} \vspace{1cm} GNU is a recursive acronym for "GNU's Not Unix!", chosen because GNU's design is Unix-like, but differs from Unix by being free software and containing no Unix code. Richard Stallman, the founder of the project, views GNU as a "technical means to a social end". Stallman has written about "the social aspects of software and how Free Software can create community and social justice." in his "Free Society" book. \vspace{1cm} \textbf{Some Projects} \begin{multicols}{2} Fedora \url{https://getfedora.org} \includegraphics[width=1cm]{fedora.png} GNOME \url{https://getfedora.org} \includegraphics[width=1cm]{gnome.png} \end{multicols} \end{document}

The features here only scratch the surface of LaTeX capabilities. You can learn more about them at the project [help and documentation site](https://www.latex-project.org/help/).

## Chris

Nice article!

Anyway: The example in “Using Lists and Formats” has an error:

The third last line ends with two curly braces but needs three ones:

## Paul W. Frields

@Chris: Thanks, fixed!

## Mark

Does latex now automatically download required packages?

I used to download ALL of latex. This would be OK except when updating from say F25->F26 where it would have to install 4000+ latex related packages all over again.

## David Potter

As far as I know, the only version of Latex that does “on-the-fly installation” of any packages you need for a particular document is MikTex. MikTex is only available for Windows.

## Horst H. von Brand

Most packages are small (a few KiB), to update hundreds of them is no big deal.

## Son Nguyen

This article is just a beginning, a brief tutorial, but it’s still very helpful for the beginner like me. Hope you continue to write some more article about Latex tutorial.

Thank you.

## Steve

I am a Solution Provider and integrator of industrial automation products. I create all of my customers manuals using TeXstudio. I was familiar with latex, but had never really used it until I began using TeXstudio about 6 years ago. Even after 6 years of use, I have barely scratched the surface of the potential of TeXstudio, or latex for that matter. Good article Paul. These types of Open Source projects, such a TeXstudio, latex, etc… are able to compete against the proprietary over priced offerings being shilled by the giant corporations. Now if only I could program every PLC I come across with Open Source solutions I would feel … free.

## Jim

Another minor error: In the “includegraphics” example, in the text pane above the TeXstudio image the url for gnome is a duplicate of the fedora url. The Testudio image has a the correct url. At one time I used LaTeX extensively for technical documents with many mathematical formulae. Nothing does a better job of printing complex formulae correctly.

## eee

why not commandline and viewer? for example gs

## Horst H. von Brand

xemacs with AUC-TeX… 😉

## Renato

Dont worry anymore with anoinyg package installation. Use sharelatex or openleaf. Both are online editor like Google doc. Allways with the last latex and package vertion

## ken

Have been using Texmaker for creating latex documents. Would be interesting to hear

any comments on advantages and disadvantages of one over the other or whether

they complement one another.

Nice article, Will spend more time going through it

## Momme

Hi,

I want to ask how to install new packages for tex. With texlive it works with tlmgr, but I do not know how to handle it in the automtic installed tex package.

Thanks

## Horst H. von Brand

The tutorials that come with TeX are useful starting points (lshort is the “not so short” introduction to LaTeX). To dig deeper, the wikibook at https://en.wikibooks.org/wiki/LaTeX is useful, if somewhat incomplete and with errors and uses obsolete packages or some that (to my taste!) aren’t the best options. For the full details, most everything is extensely documented, and if that doesn’t help or you are completely lost, looking around in http://tex.stackexhange.com usually saves the day.

## Frank

I do like ConTeXt MUCH more:

• Way smaller headers,

• unicode,

• almost all useful packages are already included,

• so no package conflicts,

• Lua scripting, and

• the syntax is more generalized – so the handling of several commands feels the same and is very intuitive and guessable – in contrast to LaTeX, where you have all these drawbacks.

## Mehdi

Thanks

This is a great article. TeX is really hard though and boring job! LibreOffice works much easier I think, though it might not be that under your control.

## Hiisi

Any benefit of using this redactor compare to vim or any other text editor?

## Mark

I think texstudio vs VIM is a little bit like the difference between “Text editors vs IDEs”. I wrote my thesis in TexStudio and before that, was a avid VIM user.

Here are some of the features that would have been difficult (maybe just taken alot more time to setup than I had) in VIM

Nicer formatting on error messages and warning message. Latex by no means gives you “useful” messages, but at least in TexStudio the mssages are organized and I can double click on the first one. Sometimes that was useful. Other times, it was no slower than trying to piece together an error message that was spread over multiple lines on the command line.

Cross platform. You can use it on Windows which might make collaborative projects easier since not everybody uses Linux.

You can click on the PDF preview and say “Go to source”. This was very useful during the review process.

Honestly though, for certain problems like: having greek letters in my bibtex files, TexStudio didn’t help anymore than VIM. But I think that is a latex problem, and not a TexStudio problem.