TransWikia.com

Horizontally center fractions (and expressions) in table

TeX - LaTeX Asked by Superman on January 21, 2021

I am trying to center my fractions, but it seems to be shifted upward relative to the center of the boxes. Here is the code:

documentclass[12pt]{article}
usepackage{amssymb, graphicx}
usepackage{amsmath}
usepackage{amsthm}
usepackage{float}
usepackage{enumitem}
usepackage{amsfonts,bm}
usepackage{diagbox}
usepackage[makeroom]{cancel}
usepackage{pgfplots}
usepackage{tikz}
usetikzlibrary{shapes,arrows}
usepackage{verbatim}
usepackage[american,siunitx]{circuitikz}
usepackage[export]{adjustbox}
usepackage{mathtools}
DeclarePairedDelimiterceil{lceil}{rceil}
DeclarePairedDelimiterfloor{lfloor}{rfloor}
DeclarePairedDelimiternorm{lvert}{rvert}
usepackage{units}
usepackage{relsize}
usepackage[margin=1in]{geometry} 
letDeclareUSUnitDeclareSIUnit
letUSSI
DeclareUSUnitmile{mi}
usepackage{optidef}
usepackage{interval}


intervalconfig{
    soft open fences,
    separator symbol=, ,
} 

begin{document}
    title{vspace{-2cm}HW 8}
    author{John Doe}
    date{today}
    maketitle
    begin{enumerate}[leftmargin =*]
        item Looking at $x$, we see that $T = 0.2pi$, so $x = sinleft(10tright)$. Therefore, 
        $$bm{Phi}left(tright) = begin{bmatrix} 1 & sinleft(10tright) & sin^2left(10tright) & sin^3left(10tright) & sin^4left(10tright)end{bmatrix}^T
        $$
        Next,
        $$bm{Phi}left(tright)bm{Phi}^Tleft(tright) =
        begin{bmatrix}
            1 & sinleft(10tright) & sin^2left(10tright) & sin^3left(10tright) & sin^4left(10tright)\
            sinleft(10tright) & sin^2left(10tright) & sin^3left(10tright) & sin^4left(10tright) & sin^5left(10tright)\
            sin^2left(10tright) & sin^3left(10tright) & sin^4left(10tright) & sin^5left(10tright) & sin^6left(10tright)\
            sin^3left(10tright) & sin^4left(10tright) & sin^5left(10tright) & sin^6left(10tright) & sin^7left(10tright)\
            sin^4left(10tright) & sin^5left(10tright) & sin^6left(10tright) & sin^7left(10tright) & sin^8left(10tright)
        end{bmatrix}
        $$
        Then, the PE condition is evaluated as
        $$int_{t}^{t+T}bm{Phi}left(tauright)bm{Phi}^Tleft(tauright) dtau=
        int_{t}^{t+T}
        begin{bmatrix}
            1 & sinleft(10tauright) & sin^2left(10tauright) & sin^3left(10tauright) & sin^4left(10tauright)\
            sinleft(10tauright) & sin^2left(10tauright) & sin^3left(10tauright) & sin^4left(10tauright) & sin^5left(10tauright)\
            sin^2left(10tauright) & sin^3left(10tauright) & sin^4left(10tauright) & sin^5left(10tauright) & sin^6left(10tauright)\
            sin^3left(10tauright) & sin^4left(10tauright) & sin^5left(10tauright) & sin^6left(10tauright) & sin^7left(10tauright)\
            sin^4left(10tauright) & sin^5left(10tauright) & sin^6left(10tauright) & sin^7left(10tauright) & sin^8left(10tauright)
        end{bmatrix} dtau
        $$
        We now take a look at each trig integral with different powers.
        begin{table}[H]
            centering
            begin{tabular}{|c|c|}
                hline
                $n$ & $mathlarger{intsin^nleft(10tauright)dtau}$\ [12pt]
                hline
                0 & $tau$\[12pt]
                hline
                1 & $-dfrac{cosleft(10tauright)}{10}$\[12pt]
                hline
                2 & $dfrac{tau}{2} - dfrac{sinleft(20tauright)}{40}$\[12pt]
                hline
                3 & $dfrac{frac{cos^3left(10tauright)}{3} - cosleft(10tauright)}{10}$\[12pt]
                hline
                4 & $dfrac{120tau - 8sinleft(20tauright) + sinleft(40tauright)}{320}$\[12pt]
                hline
                5 & $dfrac{-frac{cos^5left(10tauright)}{5} + frac{2cos^3left(10tauright)}{3}- cosleft(10tauright)}{10}$\[12pt]
                hline
                6 & $dfrac{600tau + 4sin^3left(20tauright) - 48sinleft(20tauright) + 9sinleft(40tauright)}{1920}$\[12pt]
                hline
                7 & $dfrac{frac{cos^7left(10tauright)}{7} - frac{3cos^5left(10tauright)}{5} + cos^3left(10tauright) - cosleft(10tauright)}{10}$\[12pt]
                hline
                8 & $dfrac{8400tau + 128sin^3left(20tauright) - 768sinleft(20tauright) + 168sinleft(40tauright) + 3sinleft(80tauright)}{30720}$\[12pt]
                hline
            end{tabular}
            caption{Trig integrals for powers of $n in [0,8] in mathbb{Z}$.}
        end{table}
        Then, with $T = 0.2pi$ and applying the integral bounds, we get the following:
        begin{table}[H]
            centering
            begin{tabular}{|c|c|}
                hline
                $n$ & $mathlarger{int_{t}^{t+T}sin^nleft(10tauright)dtau}$\ [12pt]
                hline
                0 & $0.2pi$\[12pt]
                hline
                1 & $0$\[12pt]
                hline
                2 & $0.1pi$\[12pt]
                hline
                3 & $0$\[12pt]
                hline
                4 & $dfrac{3pi}{40}$\[12pt]
                hline
                5 & $0$\[12pt]
                hline
                6 & $dfrac{pi}{16}$\[12pt]
                hline
                7 & $0$\[12pt]
                hline
                8 & $dfrac{7pi}{128}$\[12pt]
                hline
            end{tabular}
        end{table}
    end{enumerate}
end{document}

Here is the output that I currently have:

enter image description here

enter image description here

Is there a way to center them?

3 Answers

Somewhat late to the party, but hopefully still useful. Rather than focus one's efforts on achieving vertical centering between successive horizontal lines, I'd recommend getting rid of the horizontal (as well as vertical) lines. This approach also lets me get rid of all [12pt] spacers.

Next, those long fraction bars look very forbidding. I think your readers will appreciate you replacing the frac{<very long numerator>{<short denominator>} expressions with frac{1}{<short denominator>}[<very long numerator>] expressions.

I'd also use a longtable environment instead of a table/tabular combination, in order to allow an automatic page break if and when needed.

Finally, do get rid of the multitude of left and right sizing directives: They accomplish nothing -- except to mess with TeX's fine horizontal spacing rules.

enter image description here


enter image description here

documentclass[12pt]{article}
usepackage[margin=1in]{geometry} 
usepackage{mathtools, amssymb, bm, graphicx}
usepackage{array, booktabs, longtable}
setlengthLTcapwidth{textwidth}
usepackage{float,enumitem, interval}
intervalconfig{soft open fences,separator symbol=, } 

newcommandmymat{%
begin{bmatrix*}[r]
          1 &   sin(10t) & sin^2(10t) & sin^3(10t) & sin^4(10t)\
  sin(10t) & sin^2(10t) & sin^3(10t) & sin^4(10t) & sin^5(10t)\
sin^2(10t) & sin^3(10t) & sin^4(10t) & sin^5(10t) & sin^6(10t)\
sin^3(10t) & sin^4(10t) & sin^5(10t) & sin^6(10t) & sin^7(10t)\
sin^4(10t) & sin^5(10t) & sin^6(10t) & sin^7(10t) & sin^8(10t)
end{bmatrix*}}

begin{document}
title{vspace{-2cm}HW 8}
author{John Doe}
date{today}
maketitle

begin{enumerate}[left=0pt]
item Looking at $x$, we see that $T = 0.2pi$, so $x = sin(10t)$. Therefore, 
[
bm{Phi}(t) = 
begin{bmatrix} 
1 & sin(10t) & sin^2(10t) & sin^3(10t) & sin^4(10t)
end{bmatrix}^T
]
Next,
[
bm{Phi}(t)bm{Phi}^T!(t) = mymat
]

Then, the PE condition is evaluated as
[
int_{t}^{t+T}bm{Phi}(tau)bm{Phi}^T!(tau), dtau=
int_{t}^{t+T} mymat dtau
]
The following table shows the trig integrals for various powers of $n$.
begin{longtable}{@{} >{$}l<{$} >{$displaystyle}l<{$} @{}}
caption{Trig integrals for powers of $n in {0,1,dots,8} subset mathbb{Z}$.}\
toprule
n & intsin^n(10tau),dtau\ addlinespace
midrule
0 & tau\ addlinespace
1 & -frac{1}{10}cos(10tau) \ addlinespace
2 & frac{tau}{2} - frac{1}{40}sin(20tau) \ addlinespace
3 & frac{1}{10}bigl[tfrac{1}{3}cos^3(10tau) - cos(10tau)bigr] \ addlinespace
4 & frac{1}{320}bigl[120tau - 8sin(20tau) + sin(40tau)bigr] \ addlinespace
5 & frac{1}{10}bigl[-tfrac{1}{5}cos^5(10tau) + tfrac{2}{3}cos^3(10tau)- cos(10tau) bigr] \ addlinespace
6 & frac{1}{1920}bigl[600tau + 4sin^3(20tau) - 48sin(20tau) + 9sin(40tau) bigr]\ addlinespace
7 & frac{1}{10}bigl[ tfrac{1}{7}cos^7(10tau) - tfrac{3}{5}cos^5(10tau) + cos^3(10tau) - cos(10tau) bigr] \ addlinespace
8 & frac{1}{30720}bigl[8400tau + 128sin^3(20tau) - 768sin(20tau) + 168sin(40tau) + 3sin(80tau) bigr] \addlinespace 
bottomrule
end{longtable}

With $T = 0.2pi$ and applying the integral bounds, we get:
begin{longtable}{@{} >{$}l<{$} >{$displaystyle}l<{$} @{}}
toprule
n & int_{t}^{t+T} sin^n(10tau),dtau \ addlinespace
midrule
1,3,5,7 & 0 \addlinespace
0 & 0.2pi \ addlinespace
2 & 0.1pi \ addlinespace
4 & (3/40) pi \ addlinespace
6 & (1/16)pi\ addlinespace
8 & (7/128)pi \ 
bottomrule
end{longtable}
end{enumerate}
end{document}

Answered by Mico on January 21, 2021

In addition to the excellent solution provided by @muzimuzhi regarding cellspace I have also added the option of makegapedcell -- please have a look

You could add addlinespace as a third option to get extra space with the booktabs package but the downside is you would have to remove the hline which would show gaps otherwise

I have removed some packages and also the n in the first column since unicode errors were being thrown-- I am sure you will recover

enter image description here

MWE

documentclass{article}
usepackage{amsmath,amsfonts}
usepackage{booktabs}
usepackage[column=o]{cellspace}%
setlengthcellspacetoplimit{5pt}
setlengthcellspacebottomlimit{5pt}
usepackage{makecell}
setcellgapes{3pt}
usepackage[american,siunitx]{circuitikz}
usepackage{mathtools}
usepackage{relsize}
usepackage[margin=1in]{geometry} 
letDeclareUSUnitDeclareSIUnit
letUSSI
DeclareUSUnitmile{mi}

begin{document}
begin{table}[!htbp]
            centering
            begin{tabular}{|oc|oc|}
                hline
                & $mathlarger{intsin^nleft(10tauright)dtau}$\
                hline
                0 & $tau$\
                hline
                1 & $-dfrac{cosleft(10tauright)}{10}$\
                hline
                2 & $dfrac{tau}{2} - dfrac{sinleft(20tauright)}{40}$\
                hline
            end{tabular}
end{table}
begin{table}[!htbp]            
            centeringmakegapedcells
            begin{tabular}{|c|c|}
                hline
                & $mathlarger{intsin^nleft(10tauright)dtau}$\
                hline
                0 & $tau$\
                hline
                1 & $-dfrac{cosleft(10tauright)}{10}$\
                hline
                2 & $dfrac{tau}{2} - dfrac{sinleft(20tauright)}{40}$\
                hline
            end{tabular}
end{table}
end{document} 

Answered by js bibra on January 21, 2021

A cellspace package solution, in which

  • all [12pt] used in \[12pt] is deleted, since this only adds vertical space to bottom of tabular cells.
  • cellspace is loaded and two length changed
documentclass{article}
usepackage{amsfonts}
usepackage{amsmath}
usepackage{float}
usepackage{relsize}

usepackage[column=O]{cellspace}
setlength{cellspacetoplimit}{5pt}
setlength{cellspacebottomlimit}{5pt}

begin{document}
begin{table}[H]
  centering
  begin{tabular}{|Oc|Oc|}
      hline
      $n$ & $mathlarger{intsin^nleft(10tauright)dtau}$\ 
      hline
      0 & $tau$\
      hline
      1 & $-dfrac{cosleft(10tauright)}{10}$\
      hline
      2 & $dfrac{tau}{2} - dfrac{sinleft(20tauright)}{40}$\
      hline
      3 & $dfrac{frac{cos^3left(10tauright)}{3} - cosleft(10tauright)}{10}$\
      hline
      4 & $dfrac{120tau - 8sinleft(20tauright) + sinleft(40tauright)}{320}$\
      hline
      5 & $dfrac{-frac{cos^5left(10tauright)}{5} + frac{2cos^3left(10tauright)}{3}- cosleft(10tauright)}{10}$\
      hline
      6 & $dfrac{600tau + 4sin^3left(20tauright) - 48sinleft(20tauright) + 9sinleft(40tauright)}{1920}$\
      hline
      7 & $dfrac{frac{cos^7left(10tauright)}{7} - frac{3cos^5left(10tauright)}{5} + cos^3left(10tauright) - cosleft(10tauright)}{10}$\
      hline
      8 & $dfrac{8400tau + 128sin^3left(20tauright) - 768sinleft(20tauright) + 168sinleft(40tauright) + 3sinleft(80tauright)}{30720}$\
      hline
  end{tabular}
  caption{Trig integrals for powers of $n in [0,8] in mathbb{Z}$.}
end{table}
end{document}

cellspace package example

Answered by muzimuzhi Z on January 21, 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