LaTeX 環境 環境

LaTeX環境 環境

原文:Environments
翻訳者:Xovee
翻訳時期:2023年4月16日

LaTeX 環境は、文書の特定の部分をフォーマットするために使用されます。この記事では、既存の LaTeX 環境の使用方法と、新しい LaTeX 環境を定義する方法について説明します。

導入

環境\begin{name}は と によって宣言されます\end{name}

\begin{name}
Your content here...
...goes here...
\end{name}

ここでname、 は環境の名前です。たとえば、次の例では、center環境を使用して段落のテキストを中央に配置します。

\documentclass{article}
\begin{document}
\begin{center}
This is a demonstration of the \texttt{center} environment. 
This paragraph of text will be \textit{centred} because it is 
contained within a special environment. Environments provide 
an efficient way to modify blocks of text within your document.
\end{center}
\end{document}

ここに画像の説明を挿入

環境

次の例では、tabular環境を使用してテーブルを表示します。{c c c}この環境は、テーブル内のコンテンツの配置を定義する追加パラメーターを受け入れます。テーブルの作成の詳細については、この記事を参照してください。

\documentclass{article}
\begin{document}
\begin{tabular}{ c c c } 
  cell1 & cell2 & cell3 \\ 
  cell4 & cell5 & cell6 \\ 
  cell7 & cell8 & cell9 \\ 
 \end{tabular}
\end{document}

ここに画像の説明を挿入
ほとんどの環境コマンドは、角括弧で囲まれた引数を受け入れます。

新しい環境を定義する

以下を使用して新しい環境を定義します\newenvironment

\newenvironment{name}[numarg][optarg_default]{begin_def}{end_def}

で:

  • nameは指定した環境名です
  • numarg引数の数 (1 ~ 9) です。指定しない場合numarg、環境はパラメータ入力を受け入れません。たとえばboxed環境。
  • optarg_default環境の最初のパラメータをオプションにし、そのデフォルト値を設定します。つまり、ユーザーがオプションのパラメーターの値を入力しない場合、環境ではデフォルト値が使用されます。
  • begin_def環境の開始時に実行するコードを定義します。ここでは、などの環境パラメータを使用できます#1#2
  • end_def環境の終了時に実行されるコードを定義します。ただし、ここでは環境のパラメーターを使用できません。

単純な環境を定義する

この例では、boxed環境を定義します。パラメータは受け入れられません。その機能はテキストにボックスを追加することです。

\documentclass{article}
%We can define the environment in the preamble
\newenvironment{boxed}
    {\begin{center}
    \begin{tabular}{|p{0.9\textwidth}|}
    \hline\\
    }
    { 
    \\\\\hline
    \end{tabular} 
    \end{center}
    }
%
\begin{document}
Now we can use the \texttt{boxed} environment in our document:

\begin{boxed}
This text is formatted within the \texttt{boxed} environment.
\end{boxed}

This text is typeset outside the \texttt{boxed} environment.
\end{document}

ここに画像の説明を挿入
\newenvironment 次に、この環境を定義する方法を分析してみましょうboxed

  • nameはいboxed
  • 使用されていない[numarg]、または[optarg_default]
  • begin_def中括弧内で定義すると、内部のコードが環境の先頭で実行されます。
\begin{center}
\begin{tabular}{|p{0.9\textwidth}|}
\hline\\
  • end_def中括弧内で定義すると、環境の終了時に内部のコードが実行されます。
 \\\\\hline
 \end{tabular} 
 \end{center}

この例では、環境begin_defが定義されており、その環境内に、環境の初期コード(テキストの外側に横線と縦線を描画する)が定義されています。水平線が定義され、その後に環境と環境の終了コードが続きます。centercentertabularend_deftabularcenter

許容可能なパラメータを定義する環境

前の例に基づいて、パラメーターを受け入れることができる環境を定義しますboxed

  • boxed環境のタイトルを定義するオプションのパラメータを取ります。ユーザーがオプションのパラメータを入力しない場合、デフォルトのタイトルが使用されます。This is a box
  • 2 番目のパラメーターの場合、このパラメーターの効果は、パラメーターの内容を環境コンテンツの先頭に表示することです。

したがって、パラメータが 2 つあります。

  • 最初のパラメータはオプションです
  • 2 番目のパラメータは必須です

同時にnumarg2 に設定する必要があります。

更新された環境は次のとおりですboxed

\newenvironment{boxed}[2][This is a box]
    {\begin{center}
    Argument 1 (\#1)=#1\\[1ex]
    \begin{tabular}{
   
   {
   
   {!}}p{0.9\textwidth}{
   
   {!}}}
    \hline\\
    Argument 2 (\#2)=#2\\[2ex]
    }
    { 
    \\\\\hline
    \end{tabular} 
    \end{center}
    }
  • [numarg]新しい値セットを追加しました2
  • 新しいパラメーターを追加し[optarg_default]、最初のパラメーターをオプションにし、デフォルト値 ie を提供しましたThis is a box

次の例は、新しく定義された環境の使用法を示しています。

\documentclass{article}
% Note the default value for the first
% argument is provided by [This is a box]
\newenvironment{boxed}[2][This is a box]
    {\begin{center}
    Argument 1 (\#1)=#1\\[1ex]
    \begin{tabular}{|p{0.9\textwidth}|}
    \hline\\
    Argument 2 (\#2)=#2\\[2ex]
    }
    { 
    \\\\\hline
    \end{tabular} 
    \end{center}
    }
\begin{document}
\textbf{Example 1}: Use the default value for the first argument:
 
\begin{boxed}{Some preliminary text}
This text is \textit{inside} the environment.
\end{boxed}

This text is \textit{outside} the environment.

\vskip12pt

\textbf{Example 2}: Provide a value for the first argument:
 
\begin{boxed}[This is not the default value]{Some more preliminary text}
This text is still \textit{inside} the environment.
\end{boxed}

This text is also \textit{outside} the environment.
\end{document}

ここに画像の説明を挿入

番号付き環境

カウンタを手動で作成することも、\newtheorem環境を使用して番号付き環境を実装することもできます。

\documentclass{article}
% Define our numbered environment within the preamble
\newcounter{example}[section]
\newenvironment{example}[1][]{\refstepcounter{example}\par\medskip
   \noindent \textbf{Example~\theexample. #1} \rmfamily}{\medskip}

% Another numbered environment defined with \newtheorem
\usepackage{amsmath} % For the \newtheorem command
\newtheorem{SampleEnv}{Sample Environment}[section]
\begin{document}

\section{User-defined numbered environments}

\begin{example}
First user-defined numbered environment (number \theexample).
\end{example}

\begin{example}
Second user-defined numbered environment (number \theexample).
\end{example}

\section{More user-defined numbered environments}
Note how the example numbering has restarted at 1:

\begin{example}
First user-defined numbered environment (number \theexample).
\end{example}

\begin{SampleEnv}
User-defined environment created with the \verb|\newtheorem| command.
\end{SampleEnv}
\end{document}

ここに画像の説明を挿入
予防:

  • この環境ではexample、コマンドは\newcounter{example}[section]という名前のカウンターを作成しますexampleこのカウンター:
    • コマンドを使用してrefstepcounter{example}カウンタの数を増やします(1回1
    • \section{...}スタートするたびにカウンターをリセットする
    • 新しい変数。\theexample経由でアクセスします。カウンタの詳細については、この記事を参照してください。
  • このコマンドは\newtheorem番号付け環境を直接作成し、次の 3 つのパラメーターを受け取ります。
    • 環境の名前 (この例ではSampleEnv)
    • 環境の表示タイトル (この場合は太字Sample Environment)
    • カウンタのリセットのルールを決定するオプションのパラメータ (この場合、カウンタの値はsection環境によって変化します)

\newtheorem環境に加えて、amsthmパッケージとamsmathパッケージは他の多くの数学的環境を提供します。

既存の環境を再定義する

このコマンドを使用して既存の環境を再定義できます\renewenvironment 。このコマンドの構文は\newenvironment次と同じです。

\newenvironment{name}[numarg][optarg_default]{begin_def}{end_def}

次の例は、環境を再定義する方法を示していますitemize(単なる例です)。新しいitemize環境では、テキストが中央に配置され、フォントが斜体になります。

\documentclass{article}
% Redefine the environment in the preamble
\renewenvironment{itemize}
{\begin{center}\em}
{\end{center}}
\begin{document}

\begin{itemize}
We have redefined the \texttt{itemize} environment so that any text 
within it is centred and emphasised (italicized). It no longer creates
a bulleted list---this is only an example and not intended for use 
in real documents!
\end{itemize}
\end{document}

ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/xovee/article/details/130179587