我正在尝试创建一个通用命令来编写实心标题。标题的宽度应适合其呈现标题的包含环境。
在下面的例子中,橙色标题超出了表格范围。
我应该使用什么命令来替换\textwidth
solidheading 以便它始终打印适合段落宽度或表格列内的宽度?
产生该结果的代码如下:
\documentclass[12pt]{article}
\usepackage{xcolor}
\newcommand{\solidheading}[2][yellow]{
\noindent \colorbox{#1}{\parbox[t]{\textwidth}{#2}}
\vskip1ex
}
\usepackage{colortbl}
\usepackage{fontawesome5}
\newenvironment{notes}
{ \noindent
\begin{tabular}{!{\color{black}\vrule width 0.01\textwidth}p{0.85\textwidth}}
{\cellcolor{gray}}
{\Large\faEdit[regular]} \bfseries{Notes}\\
{\cellcolor{lightgray}}
}
{
\end{tabular}
}
\begin{document}
\solidheading[pink]{Heading outside Table}
\begin{notes}
\solidheading[orange]{Don't forget}
Smile and say hello.\newline
Happy makes happy.
\end{notes}
\end{document}
答案1
您可以使用\linewidth
:\textwidth
\newcommand{\solidheading}[2][yellow]{
\noindent \colorbox{#1}{\parbox[t]{\linewidth}{#2}} % \textwidth <==============
\vskip1ex
}
附完整代码
\documentclass[12pt]{article}
\usepackage{xcolor}
\newcommand{\solidheading}[2][yellow]{
\noindent \colorbox{#1}{\parbox[t]{\linewidth}{#2}} % \textwidth
\vskip1ex
}
\usepackage{colortbl}
\usepackage{fontawesome5}
\newenvironment{notes}
{ \noindent
\begin{tabular}{!{\color{black}\vrule width 0.01\textwidth}p{0.85\textwidth}}
{\cellcolor{gray}}
{\Large\faEdit[regular]} \bfseries{Notes}\\
{\cellcolor{lightgray}}
}
{
\end{tabular}
}
\begin{document}
\solidheading[pink]{Heading outside Table}
\begin{notes}
\solidheading[orange]{Don't forget}
Smile and say hello.\newline
Happy makes happy.
\end{notes}
\end{document}
结果如下: