2

I'm trying to create a generic command to write a solid heading. The width of the heading should fit into the containing environment in which the heading it rendered.

In the example below, the orange heading flows outside the table.

What command should I use to replace \textwidthin the solidheading so that it will always print a width that fits into either a paragraph width or inside a table column?

enter image description here

The code to produce this is as follows:

\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}
2

You can use \linewidth instead of \textwidth:

\newcommand{\solidheading}[2][yellow]{
    \noindent \colorbox{#1}{\parbox[t]{\linewidth}{#2}} % \textwidth <==============
    \vskip1ex
}

With the complete code

\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}

you the result:

result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.