6

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?

  • There's a package dedicated to it: cellspace . Do you want to reinvent the wheel? :o) – Bernard Mar 11 '17 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 Mar 11 '17 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 Mar 11 '17 at 14:24
  • @Bernard -- That seems to work. Thank you. What about the second one: ` -0.4*\baselineskip - #2` – bubba Mar 11 '17 at 14:40
  • 1
    Similar: \rule[-\dimexpr 0.4\baselineskip+#2\relax]{0pt}{0pt} – Bernard Mar 11 '17 at 14:59
5

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 either 1) use eTeX macros like \dimexpr, \glueexpr, 2) the calc package or 3) the pgfmath package.

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
| improve this answer | |

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.