我定义了一个环境,用于将标准文本形状更改为\small
和\itshape
。然后,我在此环境中添加了一个表,并希望它具有与环境中定义的外观相同的外观。
如果不单独修改表格,这是否可行?如果可以,如何实现?同样的问题也适用于环境中的图形(或任何其他浮点数)。此外,如果表格能够适应环境中较小的文本宽度,那就太好了(查看表格标题以了解我的意思)。
梅威瑟:
\documentclass{article}
%define environment
\newcounter{examplecounter}
\newenvironment{example}{\small\begin{quote}%
\refstepcounter{examplecounter}%
\textbf{Example \arabic{examplecounter}}%
\quad%
\itshape%
}{%
\end{quote}%
}
\begin{document}
This is normal text.
\begin{example}
This is small and italics text.
\begin{table}[htp]
\centering
\begin{tabular}{lrr}
\hline
Parameters & Values & Description \\
\hline
$t$ & 2 & Time\\
$c$ & 0.01 & Bond coupon\\
\hline
\end{tabular}
\caption{This and the table contents should be small and italics
as well.}
\label{tab1}
\end{table}
\end{example}
\end{document}
答案1
此变体重新定义table
,使其行为正常,除非在example
选择小斜体字体的环境中使用。重新定义的理由table
是已经有很多不应该触碰的表格(请参阅原始帖子下方的讨论)。
\documentclass{article}
\newcounter{examplecounter}
\newif\ifInExample
\newenvironment{example}%
{\small\begin{quote}%
\refstepcounter{examplecounter}%
\textbf{Example \arabic{examplecounter}}%
\quad
\itshape
\InExampletrue
}%
{\end{quote}%
}
\makeatletter
\let\tableorig\table
\let\endtableorig\endtable
\renewenvironment{table}%
{\ifInExample
\let\@floatboxresetorig\@floatboxreset
\def\@floatboxreset{\@floatboxresetorig\small\itshape}%
\fi
\begin{tableorig}%
}%
{\end{tableorig}%
}
\makeatother
\begin{document}
This is normal text.
\begin{example}
This is small and italics text.
\begin{table}[htp]
\centering
\begin{tabular}{lrr}
\hline
Parameters & Values & Description \\
\hline
$t$ & 2 & Time \\
$c$ & 0.01 & Bond coupon \\
\hline
\end{tabular}
\caption{This and the table contents are small and italics.}
\label{tab1}
\end{table}
\end{example}
\begin{table}[htp]
\centering
\begin{tabular}{lrr}
\hline
Parameters & Values & Description \\
\hline
$t$ & 2 & Time \\
$c$ & 0.01 & Bond coupon \\
\hline
\end{tabular}
\caption{This and the table contents are normalsize and upright.}
\label{tab2}
\end{table}
\end{document}
答案2
这肯定是错误的标记,浮动的设计与当前环境无关,最好使用设置样式的命令,例如,无论浮动保存在何处,但无论如何,如果您禁用规范化环境的命令,则会选择当前环境。
\documentclass{article}
\makeatletter
\def\hmmm{\let\@parboxrestore\relax\let\@floatboxreset\relax}
\makeatother
%define environment
\newcounter{examplecounter}
\newenvironment{example}{\small\begin{quote}%
\refstepcounter{examplecounter}%
\textbf{Example \arabic{examplecounter}}%
\quad%
\itshape%
}{%
\end{quote}%
}
\begin{document}
This is normal text.
\begin{example}
This is small and italics text.
\hmmm
\begin{table}[htp]
\centering
\begin{tabular}{lrr}
\hline
Parameters & Values & Description \\
\hline
$t$ & 2 & Time\\
$c$ & 0.01 & Bond coupon\\
\hline
\end{tabular}
\caption{This and the table contents should be small and italics
as well.}
\label{tab1}
\end{table}
\end{example}
\end{document}
beware\@parboxrestore
也用于 parboxes 和表p
列中,因此如果您不希望它们继承环境,您可能需要在表内重置此命令。
答案3
不要试图将非浮动元素强行table
塞入example
环境,而是加载caption
包并使用宏\captionof{table}{...}
来创建表格的标题。该caption
包还允许您更改标题的字体大小和形状。另外,我建议您使用居中minipage
环境,而不是quote
包含example
环境材料的环境。
\documentclass{article}
\usepackage{caption} % for "\captionof" and "\captionsetup" macros
\usepackage{lipsum} % for filler text
% Define "example" environment
\newcounter{examplecounter}
\newenvironment{example}{%
\par
\bigskip\noindent
\refstepcounter{examplecounter}%
\hspace{0.1\textwidth}% % set indent and width
\begin{minipage}{0.8\textwidth}
\captionsetup{size=small,font=it}
\textbf{Example \theexamplecounter}%
\quad%
\itshape% %font shape for remainder of "minipage" env.
}{%
\end{minipage}
\bigskip%
}
\begin{document}
\noindent
This is normal text.
\begin{example}
This is small and italic text.
\par\centering\medskip
\begin{tabular}{lrr}
\hline
Parameters & Values & Description \\
\hline
$t$ & 2 & Time\\
$c$ & 0.01 & Bond coupon\\
\hline
\end{tabular}
\captionof{table}{This caption and the table contents
now are small and italic as well.}
\label{tab1}
\end{example}
\lipsum[2] % filler text
\end{document}