针对此例:新环境中没有括号的新命令,我必须将当前字体大小传递给环境,但我更喜欢它自己采用它?
为了清楚起见,我想存储selectfont
参数并在以后重复使用它们。
有一个明显的括号解决方案,但是相关问题的 OP 不想使用括号。
\documentclass{article}
\pagestyle{empty}
\begin{document}
\begin{itemize}
\item Bla
\item{\small Small is small}
\item Bla is not small, but the OP of the related question doesn't want to use brackets.
\end{itemize}
\end{document}
然后,我尝试在\item
宏内创建一个切换系统,为此我必须知道字体大小参数是什么,以便存储和恢复它们。
\documentclass{article}
\pagestyle{empty}
\usepackage{paralist,xparse}
\newcommand{\Com}[1][\hspace{1cm}]{\item[#1\aftergroup\footnotesize]}
\NewDocumentEnvironment{Itemize}
{O{} % item caracter ex $\bullet$
D<>{\normalsize}}{%
\begin{list}{#2#1\aftergroup#2}{%
\setlength{\labelwidth}{0em} % adapt as you want
\setlength{\leftmargin}{0pt}
\setlength{\itemindent}{5pt}%
}}{%
\end{list}}
\begin{document}
\large
\begin{Itemize}[$\bullet$]<\large>
\item One
\item Two
\Com This is my comment
\item Three
\Com This is my comment
\end{Itemize}
Back to current font size !
\end{document}
答案1
\documentclass{article}
\usepackage{lipsum}
\usepackage{pgf}
\usepackage{pgffor}
%This is my suggestion (with @Circumscribe's comment in account):
\makeatletter
\newcommand{\savefont}{\xdef\oldfontsize{\f@size}\xdef\oldblskip{\f@baselineskip}}
\newcommand{\backtoppevfont}{\fontsize{\oldfontsize}{\oldblskip}\selectfont}
\makeatother
%Just added to demonstrate "unknown" font
\def\randomSize{%
\pgfmathsetmacro{\a}{random(1,4)}%
\foreach \fnt[count=\i from 1] in {\tiny,\footnotesize,\large,\Large}{%
\ifnum \a=\i\global\let\fs\fnt\breakforeach
\fi}\expandafter\string\fs\fs%
}
\begin{document}
Here is a text... and a random change of font to:\randomSize'' Test! RFont \savefont
That turned into \small\verb|\small| here and restored to \backtoppevfont RFont1!
\end{document}
输出:
答案2
保存当前字体并添加一些代码,\item
以便在发出所需的代码后恢复字体\par
。
\documentclass{article}
\usepackage{etoolbox}
\usepackage[nopar]{lipsum} % mock text
\newif\ifcomment
\newcommand{\Com}{\par\footnotesize\itshape\commenttrue}
\newenvironment{Itemize}
{%
\edef\Itemizecurrent{\the\font}%
\itemize
\preto{\item}{\ifcomment\par\Itemizecurrent\fi}%
}{%
\enditemize
}
\begin{document}
\begin{Itemize}
\item One
\item \lipsum[4]
\Com \lipsum[5]
\item Three
\Com This is my comment
\end{Itemize}
Back to current font size!
\end{document}