在环境中包装LaTeX命令 [英] wrap LaTeX command in environment

查看:93
本文介绍了在环境中包装LaTeX命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在环境中包装LaTeX命令?本质上,如何将\ somecommand {contents}转换为\ begin {somecommand}内容\ end {somecommand}?我已经尝试过在创建新环境中尝试显而易见的方法:

How can I wrap a LaTeX command in an environment? In essence, how can I turn \somecommand{contents} into \begin{somecommand} contents \end{somecommand}? I have tried the obvious in creating a new environment as such:

\newenvironment{somecommand}[0]{
  \somecommand{
}
{
  }
}

但这会引起大括号问题.让我举一个更具体的例子.假设您要创建非常重要的环境,并且要使用命令Emph来完成此任务.一个简单(但不正确)的解决方案是将内容写为

but this causes problems with the curly brackets. Let me give a more concrete example. Say that you want to create the environment very-important and you want to use the command emph to accomplish this. An straightforward (but incorrect) solution would be to write something as

\newenvironment{very-important}[0]{
  \emph{
}
{
  }
}

这里的问题是该命令与在环境中找到的信息一起使用,因此它不是环境的打开命令之一,也不是环境的关闭命令.然后的问题是:如何做到这一点?

The problem here is that the command works with the information that is found inside the environment, so it is not one of the opening commands of the environment, nor is it a closing command of the environment. The question is then: how can you do this?

推荐答案

似乎现在我可以猜到问题是什么了.

It seems that now I can guess what is the question.

\newenvironment{very-important}{\startimportant}{}
\def\startimportant#1\end{\emph{#1}\end}

\begin{very-important}
Something
\end{very-important}

此解决方案效果很好. 但是恕我直言,将所有文本包装在环境中是个坏主意.为什么? 有两种方法可以对文本进行处理. 例如,您要更改字体并使用斜体.

This solution works well. But IMHO it is bad idea to wrap all text in the environment. Why? There are two ways to do something with the text. For example, you want to change the font and use italic.

  • 第一种方法. \textit{sentence written in italics}
  • 第二种方法. {\it sentence written in italics\/}
  • The first method. \textit{sentence written in italics}
  • The second method. {\it sentence written in italics\/}

有什么区别?关键是第一种方法使用第二种方法. \it宏更改字体,而括号}将其更改回. \textit宏读取完整的参数,更改字体并再次插入参数: \textit大致定义如下(不完全正确).

What is the difference? The thing is that first method use the second one. \it macro changes the font and the brace } changes it back. \textit macro reads the full argument, changes the font and inserts the argument again: \textit is defined roughly as follows (not exactly).

\def\texit#1{{\it#1\/}}

第一种方法总是做额外的工作.它读取参数两次. 几乎总是可以进行更改,然后可以恢复所有内容.

The first method is always doing extra work. It reads the argument twice. Almost always, you can make changes and then you can everything back.

最终您为什么要使用环境?使用宏.

Eventually why do you use the environment? Use macros.

\veryimportant{
   Any thought
}

这篇关于在环境中包装LaTeX命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆