\newenvironment
我有一个文档,其中我使用改编自的来为各个部分添加颜色这个答案。虽然它似乎在 430 个地方运行良好,但在一个案例中,它通过添加一条额外的线(下图中的 2.)提供了意想不到的结果。
虽然我有办法解决这个问题,但也许有一个解决办法,因为我还有很多文档工作要做,这可能会导致问题的转移。
注意:我也在段落中使用环境,没有问题。例如:Some words \begin{LII}{more words}\end{LII} end of paragraph.
\documentclass[11pt,letterpaper,oneside,onecolumn]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[margin=1in,showframe]{geometry}
\usepackage{parskip}
\renewcommand\familydefault\sfdefault
\usepackage{xcolor}
\newenvironment{LII}{%% https://tex.stackexchange.com/a/207228/187997
\leavevmode\color{cyan}\ignorespaces%
}{%
}%
\begin{document}
1. Line before, no color.
(2) Identifier-1 must be defined as an alphanumeric data item such that its value can be a program name.
Line after.
2. Line before, color environment above and below paragraph.
\begin{LII}{%
(2) Identifier-1 must be defined as an alphanumeric data item such that its value can be a program name.
}\end{LII}
Line after.
3. Line before, color environment above and below with \% at end of paragraph.
\begin{LII}{%
(2) Identifier-1 must be defined as an alphanumeric data item such that its value can be a program name.%
}\end{LII}
Line after.
4. Line before, color environment within paragraph.
\begin{LII}{(2) Identifier-1 must be defined as an alphanumeric data item such that its value can be a program name.}\end{LII}
Line after.
\end{document}
答案1
如果我添加一个取消跳过,您的示例就可以很好地编译:
\documentclass[11pt,letterpaper,oneside,onecolumn]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[margin=1in,showframe]{geometry}
\usepackage{parskip}
\renewcommand\familydefault\sfdefault
\usepackage{xcolor}
\newenvironment{LII}{%% https://tex.stackexchange.com/a/207228/187997
\leavevmode\color{cyan}\ignorespaces%
}{\unskip%
}%
\begin{document}
1. Line before, no color.
(2) Identifier-1 must be defined as an alphanumeric data item such that its value can be a program name.
Line after.
2. Line before, color environment above and below paragraph.
\begin{LII}{%
(2) Identifier-1 must be defined as an alphanumeric data item such that its value can be a program name.
}\end{LII}
Line after.
3. Line before, color environment above and below with \% at end of paragraph.
\begin{LII}{%
(2) Identifier-1 must be defined as an alphanumeric data item such that its value can be a program name.%
}\end{LII}
Line after.
4. Line before, color environment within paragraph.
\begin{LII}{(2) Identifier-1 must be defined as an alphanumeric data item such that its value can be a program name.}\end{LII}
Line after.
\end{document}
答案2
\documentclass[11pt,letterpaper,oneside,onecolumn]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[margin=1in,showframe]{geometry}
\usepackage{parskip}
\renewcommand\familydefault\sfdefault
\usepackage{xcolor}
\newenvironment{LII}{%% https://tex.stackexchange.com/a/207228/187997
\par\leavevmode\color{cyan}\ignorespaces%
}{%
\par
}%
\begin{document}
1. Line before, no color.
(2) Identifier-1 must be defined as an alphanumeric data item such that its value can be a program name.
Line after.
2. Line before, color environment above and below paragraph.
\begin{LII}{%
(2) Identifier-1 must be defined as an alphanumeric data item such that its value can be a program name.
}\end{LII}
Line after.
3. Line before, color environment above and below with \% at end of paragraph.
\begin{LII}{%
(2) Identifier-1 must be defined as an alphanumeric data item such that its value can be a program name.%
}\end{LII}
Line after.
4. Line before, color environment within paragraph.
\begin{LII}{(2) Identifier-1 must be defined as an alphanumeric data item such that its value can be a program name.}\end{LII}
Line after.
\end{document}