I would be careful to redefine the tabular
environment altogether, as it may be used in different contexts than a table (for example to typeset the author name and address). Since probably the tables to which you want to add the background color are in table
environments, we'll patch only those.
\documentclass[10pt,twoside,letterpaper,openright]{scrbook}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{booktabs}
\usepackage{xpatch,letltxmacro}
%% A storage bin
\newsavebox{\tabularbox}
%% We patch the tabular environments only inside table
\AtBeginEnvironment{table}{%
\LetLtxMacro\tabular\colortabular
\LetLtxMacro\endtabular\endcolortabular
}
%% Start: save a copy of \tabular and \endtabular
\LetLtxMacro\colortabular\tabular
\LetLtxMacro\endcolortabular\endtabular
%% Patch the copies
\xpretocmd{\colortabular}
{\setbox\tabularbox=\hbox\bgroup} % start building a box
{}{}
\xapptocmd{\endcolortabular}
{%
\egroup % finish the box
\begingroup
\fboxsep=0pt % no padding
\colorbox{black!30}{\box\tabularbox}% typeset the box on a background
\endgroup
}
{}{}
\begin{document}
\chapter{Lorem Ipsum}
\begin{table}[h]
\centering
\caption{Test Table}\label{testtable}
\begin{tabular}{ll}
\toprule
Test 1 & Test 2 \\
Test 3 & Test 4 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
Like this?
\documentclass{article}
\usepackage{xcolor}
\newcommand{\sectioncolored}[3]{%
\section{\textcolor{#1}{#2} #3}%
}
\makeatletter
\def\entrylistsplitfirst#1 #2\@nil{#1}
\def\entrylistsplitsecond#1 #2\@nil{#2}
\newenvironment{entrylist}[2]{%
\edef\tempa{\expandafter\expandafter\expandafter\entrylistsplitfirst #2\@nil}%
\edef\tempb{\expandafter\expandafter\expandafter\entrylistsplitsecond #2\@nil}%
\sectioncolored{#1}{\tempa}{\tempb}%
}{}
\makeatother
\begin{document}
\begin{entrylist}{blue}{Blue Black}
abc
\end{entrylist}
\end{document}
Explanation
We need to pass \sectioncolored
3 arguments rather than 2: the colour; the first word; the remaining words. We then use \textcolor{#1}{#2}
to colour just the first word.
\newcommand{\sectioncolored}[3]{%
\section{\textcolor{#1}{#2} #3}%
}
We're going to use @
commands, so we need to change the catcode using \makeatletter
.
\makeatletter
We define \entrylistsplitfirst#1 #2\@nil
to get the first word of the title.
\def\entrylistsplitfirst#1 #2\@nil{#1}
We define \entrylistsplitsecond#1 #2\@nil
to get the remainder of the title.
\def\entrylistsplitsecond#1 #2\@nil{#2}
We need \def
because our parameter specification includes a space. The \@nil
is used to mark the end of the arguments. Note that these definitions assume that the title will have at least 2 words. If you might have a single-word title, you would need to adjust the code to accommodate this possibility as well.
We then define the environment.
\newenvironment{entrylist}[2]{%
First, we save the first word of the title into the macro \tempa
.
\edef\tempa{\expandafter\expandafter\expandafter\entrylistsplitfirst #2\@nil}%
Second, we save the rest of the title into the macro \tempb
.
\edef\tempb{\expandafter\expandafter\expandafter\entrylistsplitsecond #2\@nil}%
Finally, we pass the colour (#1
) and the two parts of the title off to \sectioncolored
. Note that I altered something here as the original code defined one macro and used another, which obviously won't work. (\sectioncolored
is not the same macro as \sectionColored
.)
\sectioncolored{#1}{\tempa}{\tempb}%
}{}
Finally, we restore @
to its usual status in the preamble.
\makeatother
Best Answer
You would need to patch the macro
\taggedline
which is the internal representation for the begin of the environment with the same name. However, because this environment has an optional argument, patching this macro is a bit tricky using the\patchcmd
macro of theetoolbox
package.But you can use
\xpatchcmd
macro of thexpatch
package for this. Your code will be much shorter then:The output is the same as the output in your question.