3. Latex Grammar Basics: Commands and Environment

3. Latex Grammar Basics: Commands and Environments

We have done all the preparations to start writing Latex, starting from this chapter, we will start to explain Latex grammar

This chapter will explain the basics of Latex syntax: commands and environments

1. Command and environment

Order

what is an order

Unlike other programming languages ​​(C/C++, Python, etc.) that use keywords, functions, and classes to implement programs,Most of the Latex grammar exists in the form of commands

And each command will have specific functions, such as title production, directory production, or setting the encoding format of the document, etc.

command format

The commands in Latex have the following three forms:

  1. no parameters

    \commad
    

    command is a specific command, such as the command to make a title, how to use it will be explained later

    \maketitle
    
  2. There are n required parameters

    \command{参数1}{参数2}...{参数n}
    
  3. There are n optional parameters

    \command[可选参数1]...[可选参数n]
    

Most commands are commands with optional parameters

Next, let's take a look at all the commands in the example just now ( note that %the beginning is a comment and will not participate in compilation )

%===============文章体裁:论文================
\documentclass[UTF8]{article}

%=================导言部分===================
%导入包
\usepackage{ctex}
\usepackage{authblk}


%标题制作
\title{论文示例}
\author{鸿神,\ Jack\ Wang}
\affil{School of Computer Science, XJTU}
\date{\today}


%=================正式开始书写===============
\begin{document}

\maketitle

\section{摘要}
这是一个LaTex 论文示例,由编写


\end{document}

The final result displayed is as follows

insert image description here

where all commands have

\documentclass[]{}
\usepackage{}
\usepackage{}
\title{}
\author{}
\affil{}
\date{}
\today
\begin{}
\maketitle
\section{}
\end{}

Each command has a different function, we will explain it later

environment

what is the environment

The content of the document we write In order to avoid the confusion of the code and the content of the text we wrote, we divide some space from the continuous context of the plain text mixed with the code and the text content. This part of the space will be an independent whole, and we can write the text content in these empty sections

For these spaces, we can use some commands in advance to set the environment of these spaces, that is, set the environment of these spaces, that is, the final display format after compilation, so the environment is actually what we set for the text of a specific part Format, this specific part can be the full text or part of the text, such as a paragraph, a picture, etc.

For example the following example(\\ means newline)

%===============文章体裁:论文================
\documentclass[UTF8]{article}
\usepackage{ctex}


\begin{document}


这是一个有article,ctex,document环境的语句\\
%下面的this具有article,ctex,documnet和textbf环境
{\textbf {this}}\\
这句话也只有documentclass和document环境,没有this环境


\end{document}

The result after compiling is as follows

insert image description here

As mentioned in the code, our commands can actually be divided into two types. The first is the command that produces the effect, such as making the title \maketitle, and the other is the command to add the environment later.

The role and scope of the environment

Just like variables in many programming languages ​​​​have scope, our environment actually has scope, specifically the scope of the text that the environment can affect

\documentclass[UTF8]{article}The scope of some environments is the full text. For example, in the above example , the genre of the article is stipulated at the beginning , and the encoding method used is UTF-8, and its scope is the full text.

The following \usepackage{ctex}indicates that we will use ctexthis package/macro package. The package/macro package is similar to the library of a programming language. There are many written functions in the library for us to use, and there are many commands that can produce various effects in the macro package. Here One of the functions of the ctex macro package used is to provide Chinese display and typesetting . Therefore, \usepackage{ctex}the scope of this command is also the full text

\beginThe and commands are used to start and end a specific environment. The sum \endwe use here is the start and end of the named environment. The content between the two commands will be affected by the environment . The role of document is to display documents . In the document environment The contents of the scope scope will be displayed after compilation.\begin{document}\end{document}documentdocument

The content in the document environment will be displayed, and the content outside the document environment will not be displayed

Usually we will set the environment of the full text \begin{document}in the front non-display area, this part is calledPreamble
For example in the small essay examples in the previous chapters

insert image description here

We guide the package in the preamble area, set the title and other content

2. Isolate context

Next, let's continue to look down at the example just now. We use the above example to introduce the isolation context

We have written such a sentence {\textbf {this}}, the function of the command \textbf is to bold the text in its scope.

Let's first look at the scope of \textbf

insert image description here

We can find that only the t is bolded in the first sentence, and the whole sentence is bolded in the second sentence, and \textbffrom the of view of the sentence itself, its scope is the following character (Note that since it can only act on one character, if the first character we bold is Chinese, an error will be reported, so Chinese characters will be reported as the first character.,As shown below)

insert image description here

But we found that there is no error in line 12. This is because == { }can not only be used as a command parameter in Latex, but also can isolate the context ==

The words wrapped by == { }(including the code) will be regarded as a character ==, so our Chinese sentence on line 12 can be successfully bolded

So for the error report that the first character above is a Chinese character, we can use { }it to enclose it

insert image description here

It is found that the error report disappears, and the effect is successfully passed through compilation.

In addition, we can also use { }to isolate the text with code, and the scope of the commands with scope will be limited in the scope of this. { }The
advantage of this is to facilitate the management of the environment. When formally writing, we usually use different paragraphs Put in different { }to manage the environment, for example (A blank line means start a new paragraph)

insert image description here

Summarize

This chapter explains what a command is, the basic format of a command, what is an environment, and how to isolate the context, etc.

In it, we have explained the functions of some commands, and we will explain these commands in detail in the next article

Guess you like

Origin blog.csdn.net/qq_45488242/article/details/110632247