9

I want to define a new command to add vertical "padding" before and after the content of a table cell. I'm stealing the idea of using rules/struts from the accepted answer to this question.

My concept is something like

\newcommand{\vpad}[2]{
   \rule{0pt}{\baselineskip + #1}              % top padding
   \rule[-0.4*\baselineskip - #2]{0pt}{0pt}}   % bottom padding

I know that there are many cases where this won't work, but that's not the issue, at present. My question is about the arithmetic expressions involving \baselineskip and the command arguments. Clearly I don't have the syntax correct. So, what is the right syntax for this sort of thing?

Edit
I'm starting to think that things like 0.4*baselineskip - #2 are not allowed. If so, maybe I have to settle for '-4pt - #2'. Is negation allowed?

5
  • There's a package dedicated to it: cellspace . Do you want to reinvent the wheel? :o)
    – Bernard
    Commented Mar 11, 2017 at 13:55
  • @Bernard -- Yes, I'm OK with re-inventing the wheel if my wheel better suits my needs. But, anyway, the real point of the question is not cell spacing, it's the syntax of arithmetic expressions.
    – bubba
    Commented Mar 11, 2017 at 14:15
  • 1
    You can try something like\rule{1pt}{\dimexpr\baselineskip+#1\relax}. But you also can load the bigstrut package, or the\setcellgapes{xx}\makegapedcells command from makecell.
    – Bernard
    Commented Mar 11, 2017 at 14:24
  • @Bernard -- That seems to work. Thank you. What about the second one: ` -0.4*\baselineskip - #2`
    – bubba
    Commented Mar 11, 2017 at 14:40
  • 1
    Similar: \rule[-\dimexpr 0.4\baselineskip+#2\relax]{0pt}{0pt}
    – Bernard
    Commented Mar 11, 2017 at 14:59

1 Answer 1

8

There are several ways to calculate equations in (La)TeX. (Therefore I created the adjcalc package as part of the adjustbox bundle so that users can select which way they want it.).

You can use

  1. the LaTeX macro \dimeval{} (requires recent kernel),
  2. eTeX macros like \dimexpr, \glueexpr,
  3. the calc package or
  4. the pgfmath package.

If you have a recent version of LaTeX, the most straightforward is probably to use \dimeval{...}.

\NewDocumentCommand{\vpad}{mm}{
   \rule{0pt}{\dimeval{\baselineskip + #1}}              % top padding
   \rule[\dimeval{-0.4*\baselineskip - #2}]{0pt}{0pt}}   % bottom padding

For older installations, the simplest thing is to use \dimexpr ... \relax. It expands to the calculation result. The \relax can be used as an endmarker which is also removed in the expansion. This requires eTeX (extended TeX) which is part of all main TeX distributions.

\newcommand{\vpad}[2]{
   \rule{0pt}{\dimexpr\baselineskip + #1\relax}              % top padding
   \rule[\dimexpr-0.4*\baselineskip - #2\relax]{0pt}{0pt}}   % bottom padding
1
  • 1
    With an up-to-date LaTeX you can use \dimeval{\baselineskip + #1} instead of \dimexpr\baselineskip + #1\relax and \dimeval{-0.4\baselineskip - #2} instead of \dimexpr-0.4*\baselineskip - #2\relax. See mirrors.ctan.org/macros/latex/base/usrguide.pdf section 5 for more information about \dimeval and similar commands.
    – cabohah
    Commented Aug 16 at 16:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .