我在新环境中重新定义命令时遇到问题。
MWE 不工作:
\documentclass{book}
\makeatletter
\newcommand{\mysection}{\@ifstar{\mysection@star}{\mysection@nostar}}
\newcommand{\mysection@star}[1]{\begingroup\sloppy\raggedright\section*{#1}\endgroup}
\newcommand{\mysection@nostar}[1]{\begingroup\sloppy\raggedright\section{#1}\endgroup}
\newcommand{\mysubsection}{\@ifstar{\mysubsection@star}{\mysubsection@nostar}}
\newcommand{\mysubsection@star}[1]{\begingroup\sloppy\raggedright\subsection*{#1}\endgroup}
\newcommand{\mysubsection@nostar}[1]{\begingroup\sloppy\raggedright\subsection{#1}\endgroup}
\makeatother
\newenvironment{MySubAppendix}{%
\makeatletter%
\renewcommand{\mysection}{\@ifstar{\mysubsection@star}{\mysubsection@nostar}}%
\makeatother%
}{}
\begin{document}
\begin{MySubAppendix}
\mysection{Sub After Block Example}
\end{MySubAppendix}
\end{document}
使用 pdflatex 会导致:
This is pdfTeX, Version 3.14159265-2.6-1.40.19 (MiKTeX 2.9.6840 64-bit)
entering extended mode
(refman.tex
LaTeX2e <2018-04-01> patch level 5
("C:\Program Files\MiKTeX 2.9\tex\latex\base\book.cls"
Document Class: book 2014/09/29 v1.4h Standard LaTeX document class
("C:\Program Files\MiKTeX 2.9\tex\latex\base\bk10.clo")) (refman.aux)
! You can't use `\spacefactor' in vertical mode.
\@->\spacefactor
\@m {}
l.19 \mysection
{Sub After Block Example}
工作MWE:
\documentclass{book}
\makeatletter
\newcommand{\mysection}{\@ifstar{\mysection@star}{\mysection@nostar}}
\newcommand{\mysection@star}[1]{\begingroup\sloppy\raggedright\section*{#1}\endgroup}
\newcommand{\mysection@nostar}[1]{\begingroup\sloppy\raggedright\section{#1}\endgroup}
\newcommand{\mysubsection}{\@ifstar{\mysubsection@star}{\mysubsection@nostar}}
\newcommand{\mysubsection@star}[1]{\begingroup\sloppy\raggedright\subsection*{#1}\endgroup}
\newcommand{\mysubsection@nostar}[1]{\begingroup\sloppy\raggedright\subsection{#1}\endgroup}
\makeatother
\newenvironment{MySubAppendix}{%
}{}
\begin{document}
\begin{MySubAppendix}
\makeatletter%
\renewcommand{\mysection}{\@ifstar{\mysubsection@star}{\mysubsection@nostar}}%
\makeatother%
\mysection{Sub After Block Example}
\end{MySubAppendix}
\end{document}
乍一看,工作版本是一种替代方案,但是当多次出现这种情况时,每次编写以下部分都会有点问题:
\makeatletter%
\renewcommand{\mysection}{\@ifstar{\mysubsection@star}{\mysubsection@nostar}}%
\makeatother%
有任何想法吗?
答案1
根据@campa 的评论,我尝试了以下操作:
\documentclass{book}
\makeatletter
\newcommand{\mysection}{\@ifstar{\mysection@star}{\mysection@nostar}}
\newcommand{\mysection@star}[1]{\begingroup\sloppy\raggedright\section*{#1}\endgroup}
\newcommand{\mysection@nostar}[1]{\begingroup\sloppy\raggedright\section{#1}\endgroup}
\newcommand{\mysubsection}{\@ifstar{\mysubsection@star}{\mysubsection@nostar}}
\newcommand{\mysubsection@star}[1]{\begingroup\sloppy\raggedright\subsection*{#1}\endgroup}
\newcommand{\mysubsection@nostar}[1]{\begingroup\sloppy\raggedright\subsection{#1}\endgroup}
\makeatother
\makeatletter%
\newenvironment{MySubAppendix}{%
\renewcommand{\mysection}{\@ifstar{\mysubsection@star}{\mysubsection@nostar}}%
}{}
\makeatother%
\begin{document}
\begin{MySubAppendix}
\mysection{Sub After Block Example}
\end{MySubAppendix}
\end{document}
这不仅在这个小例子中有效,而且在我的更大的代码中也有效。