1

I have created an environment which takes input and creates a table. A MWE of this is provided below. What I would like to do now is modify the table formatting input.

This table has a built in way of making the header row have bold text, however this method relies on appending the command \clearrow to the end of the formatting input. This is fine when the user does not provide a | at the end of the formatting, but if they do, the environment will break.

Aside from fixing the above issue, what I want is to take their formatting input and remove any preceding and trailing | and insert a | in between each column specification if it does not exist. An example of how I want the formatting to change for a few cases is provided below.

|C{0.2}L{0.2}R{0.2}L{0.4}|     ->  C{0.2}|L{0.2}|R{0.2}|L{0.4}
|C{0.2}|L{0.2}|R{0.2}|L{0.4}|  ->  C{0.2}|L{0.2}|R{0.2}|L{0.4}
C{0.25}L{0.75}                 ->  C{0.25}|L{0.75}

Is it possible to modify the input in the way I want?


Minimum Working Example

\documentclass{article}

\usepackage[table]{xcolor}      % Provides coloring for tables and text
\usepackage{tabularx}           % Customized table formatting
\RequirePackage{environ}        % Used to define custom table environment

% Define table related commands and properties
\definecolor{greyblue}{rgb}{0.6353,0.6863,0.7686} % Define a color used in the tables
\newcommand\setrow[1]{\gdef\rowmac{#1}#1\ignorespaces} % Used for making a row bold
\newcommand\clearrow{\global\let\rowmac\relax} \clearrow % Used for clearing a row formatting
\newcolumntype{C}[1]{>{\hsize=#1\hsize\rowmac\centering\arraybackslash}X} % Centered column, input is relative width of page
\newcolumntype{L}[1]{>{\hsize=#1\hsize\rowmac\raggedright\arraybackslash}X} % Left-aligned column, input is relative width of page
\newcolumntype{R}[1]{>{\hsize=#1\hsize\rowmac\raggedleft\arraybackslash}X} % Left-aligned column, input is relative width of page

% Defines an environment to create a table in the document according to
% a common formatting. This uses the environ package.
\NewEnviron{doctable}[4]{
    \begin{table}[!htbp]
        \centering
        \rowcolors{2}{black!5}{black!15}
        \begin{tabularx}{#3\linewidth}{#4<{\clearrow}}%
            \hline
            \rowcolor{greyblue} \setrow{\bfseries} % Make the header row bold and colored grey-blue
            \BODY
            \hline
        \end{tabularx}
        \caption{#2}
        \label{#1}
    \end{table}
}

\begin{document}
    \begin{doctable}
        {tbl:label}
        {Table Caption}
        {1} { C{0.25}L{0.75} }
        Heading 1 & Heading 2 \\
        Text 1    & Text 2    \\
    \end{doctable}
\end{document}
  • You could look at the xstring package. It has several macros for the manipulation and tests of strings. – Piet van Oostrum Dec 7 '16 at 16:58
  • @PietvanOostrum The xstring package looks like it has the functions to do what I want, but now I'm struggling to determine how I can use them properly. For example, instead of #4<{\clearrow} I can try \IfEndWith{#4}{|}{\StrGobbleRight{#4}{1}}{#4}<{\clearrow} to remove the end | if it exists, but this produces errors about Illegal pream-token. And I cannot find a way to edit #4 outside the tabularx input and then input to it. – zephyr Dec 8 '16 at 15:03
  • #4 will be replaced by the value of the fourth parameter. – Piet van Oostrum Dec 8 '16 at 15:29
  • @PietvanOostrum Yes, I understand that, which is why I'm trying to apply the string operations on #4. However, I cannot seem to find a way to do this that tabularx accepts. – zephyr Dec 8 '16 at 15:53

Your Answer

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

Browse other questions tagged or ask your own question.