1

I'm dynamically generating LaTeX source and compiling it. The documents contain only tables and each table cell in each column has the same width. I need to work out how many columns are required and figured the best way to do this would be to work out the total possible characters in a line and divide it by the (known) cell width. What is the best way to go about this?

1
\numexpr\textwidth/(\dimexpr2cm\relax)\relax

is the textwidth divided by 2cm. Unfortunately etex infix arithmetic rounds up so you may need to adjust by +/- 1 depending on your requirements.

The question about characters per line doesn't in general have a good answer as characters are different widths in most fonts.

| improve this answer | |
  • Is it possible to find out the width of characters in a particular font? – ajnatural Jan 21 '14 at 11:58
  • @ajnatural sure, but which characters? w is wider than i typically (except for tt monospace fonts) – David Carlisle Jan 21 '14 at 12:12
  • @ajnatural You can (roughly) calculate the average number of characters per line in a large sample of text. BTW, I did not understand why you need that count (a minimal working example can help) but I guess instead that you need really is the tabularx or tabulary packages of David Carlisle (sorry if I am wrong). – Fran Jan 21 '14 at 12:37
  • @DavidCarlisle I meant if it was possible to compute this in LaTeX somehow. – ajnatural Jan 22 '14 at 6:49
  • @Fran You may be right, my use case is slightly more complicated than I stated - however a quick look at the package description seems promising. Thanks. – ajnatural Jan 22 '14 at 6:50

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.