Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am using the following kind of 'padded array' many times:

\setlength{\fboxrule}{0pt}
\setlength{\fboxsep}{3pt}
\fbox{
\ensuremath{
\begin{array}{|l|l|}
\hline 
A & B\\ 
C & D\\
\vdots & \vdots \\
E & F\\
\hline 
\end{array}
}
} 

I'd like to be able to just write, e.g.,

\begin{DRS}
A & B\\ 
C & D\\
\vdots & \vdots \\
E & F\\
\end{DRS}

(or the same with a command instead of an environment), but I don't know a clean way to set this up. The problem is that fbox is a command, and array an environment, so I'm not sure of the right way to mix the two. I could possibly hack it together, e.g. using the environ package (which I just found by searching on this topic), but it would be nice to know the right way to do this.

Thanks!

Edit: this is the kind of image I'm producing (using nested DRSs):

enter image description here

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

If you need some padding, then ask explicitly for it:

\documentclass{article}

\newif\ifDRSmath
\newenvironment{DRS}
  {\relax\ifmmode\global\DRSmathtrue\else$\fi
   \kern3pt\begin{array}{|l|l|}
   \noalign{\kern3pt}
   \hline\mathstrut}
  {\hline
   \noalign{\kern3pt}
   \end{array}\kern3pt
   \ifDRSmath\global\DRSmathfalse\else$\fi}

\begin{document}
\begin{DRS}
    A & B\\ 
    C & D\\
    \vdots & \vdots \\
    E & F\\
\end{DRS}
\begin{DRS}
    A & B\\ 
    C & D\\
    \vdots & \vdots \\
    E & F\\
\end{DRS}

\end{document}

The conditional is needed for being able to use the environment both in text mode and in math mode. There will work in both modes: at the start we check for math mode, at the end only for \ifDRSmath and this trick will avoid issuing $ improperly.

share|improve this answer
    
Thanks... but unfortunately that doesn't produce the 3pt of padding at the top and bottom, which I need. –  Mohan Aug 17 '12 at 13:09
    
Also, is that conditional going to work with nesting? If you are already in mathmode, and enter a DRS, won't the first exit set DRSmath=false, and then the second exit (inappropriately) produce a $? –  Mohan Aug 17 '12 at 13:24
    
@Mohan I've added the requested padding (although I'm quite dubious about it). The final comments should clarify why this can be used in both text and math mode. –  egreg Aug 17 '12 at 15:17
    
Thanks... but that's producing a literal '3pt'. Could I ask what's wrong with using the fbox for the padding? –  Mohan Aug 17 '12 at 15:23
    
@Mohan Sorry, fixed. It's possible to use \fbox, with the environ package or with the lrbox environment. –  egreg Aug 17 '12 at 15:28
show 1 more comment

You can use either a normal \newenvironment (as in the DRSX environment below), or the environ package (as in the DRS environment):

enter image description here

There is no need to use \fbox{} as a normal \hline will do the job.

Code:

\documentclass{article}
\usepackage{environ}

\NewEnviron{DRS}{%
    \setlength{\fboxrule}{0pt}
    \setlength{\fboxsep}{3pt}
    $\begin{array}{|l|l|}%
        \hline
        \BODY%
        \hline
    \end{array}$%
}

\newenvironment{DRSX}{%
    \setlength{\fboxrule}{0pt}
    \setlength{\fboxsep}{3pt}
    \begin{math}\begin{array}{|l|l|}%
        \hline
}{%
    \hline
    \end{array}\end{math}%
}


\begin{document}
\begin{DRS}
    A & B\\ 
    C & D\\
    \vdots & \vdots \\
    E & F\\
\end{DRS}
\begin{DRSX}
    A & B\\ 
    C & D\\
    \vdots & \vdots \\
    E & F\\
\end{DRSX}
\end{document}
share|improve this answer
    
Thanks... but I don't follow the point about the \hline. The point of the \fbox is to put a little padding around the array. (Note \fboxrule=0pt, so no lines are drawn.) –  Mohan Aug 17 '12 at 12:01
    
PS. I'm curious about why some lines in your solution end in % but others don't... –  Mohan Aug 17 '12 at 12:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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