TransWikia.com

Set Table's Second Column Width to be Dynamic based on Page Width

TeX - LaTeX Asked by Nebabon on March 5, 2021

I’m trying to set up a table environment where it takes up the remaining width of the page. I need it to be dynamic as it will be sent to print on both A4 and Letter sized paper. I have a placeholder (with p{150mm} as it value) currently. I am not sure how to go forward as the different suggestions I’ve seen here, have not worked.

So far, I have the following:

begin{tabular}{p{25mm} p{150mm}}
    toprule
    small textbf{Title}             & textbf{tmpTestName} 
    cmidrule(r){1-1} cmidrule(l){2-2}
    small ID                         & tmpTestID 
    cmidrule(r){1-1} cmidrule(l){2-2}
    small Owner                      & tmpTestOwner 
    cmidrule(r){1-1} cmidrule(l){2-2}
    small Facility                   & tmpTestFacility 
    cmidrule(r){1-1} cmidrule(l){2-2}
    small Required HW                & tmpTestHW} 
    cmidrule(r){1-1} cmidrule(l){2-2}
    small Required S}                & tmpTestSW 
    cmidrule(r){1-1} cmidrule(l){2-2}
    small Model                      & tmpTestModel 
    cmidrule(r){1-1} cmidrule(l){2-2}
    small Description                & tmpTestDesc 
    cmidrule(r){1-1} cmidrule(l){2-2}
    small Date                       & tmpTestDate 
    bottomrule
end{tabular}

EDIT: Typo in code section was fixed

One Answer

This solution requires changepage and geometry. You can calculate the value of the second column, for odd and even pages respectively, with:

setlength{adp}{dimexpr(paperwidth-oddsidemargin-1in-25mm-4tabcolsep)}
setlength{adp}{dimexpr(paperwidth-evensidemargin-1in-25mm-4tabcolsep)}

Then you need a command (to be used inside each table) to check if the page is odd or even:

newcommand*{setcolwidth}{%
 checkoddpage
  ifoddpage
    setlength{adp}{%
      dimexpr(paperwidth-oddsidemargin-1in-25mm-4tabcolsep)}
  else
    setlength{adp}{%
      dimexpr(paperwidth-evensidemargin-1in-25mm-4tabcolsep)}
  fi
}

Here is a minimal working example. You can change the page format in the options of geometry.

documentclass{book}
usepackage[a4paper]{geometry}
%usepackage[letterpaper]{geometry}
usepackage{array,booktabs}

usepackage{changepage}
  strictpagecheck

newlength{adp}
newcommand*{setcolwidth}{%
 checkoddpage
  ifoddpage
    setlength{adp}{%
    dimexpr(paperwidth-oddsidemargin-1in-25mm-4tabcolsep)}
  else
    setlength{adp}{%
    dimexpr(paperwidth-evensidemargin-1in-25mm-4tabcolsep)}
  fi
}

begin{document}

begin{table}
  setcolwidth
  begin{tabular}{p{25mm} p{adp}}
    toprule
    small textbf{Title}             & textbf{tmpTestName} 
    cmidrule(r){1-1} cmidrule(l){2-2}
    small ID                         & tmpTestID 
    cmidrule(r){1-1} cmidrule(l){2-2}
  small Date                       & tmpTestDate 
  bottomrule
  end{tabular}
  caption{A table on an odd page}
end{table}

clearpage

begin{table}
  setcolwidth
  begin{tabular}{p{25mm} p{adp}}
    toprule
    small textbf{Title}             & textbf{tmpTestName} 
    cmidrule(r){1-1} cmidrule(l){2-2}
    small ID                         & tmpTestID 
    cmidrule(r){1-1} cmidrule(l){2-2}
    small Date                       & tmpTestDate 
    bottomrule
  end{tabular}
  caption{A table on an even page}
end{table}

end{document}

Answered by Ivan on March 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP