7

I want to draw a table like this: table

with the table width being a portion of the line width, like e.g. 0.8 * linewidth. But I cant figure out how to do it.

Anyone?

edit: MWE:

\documentclass[a4paper,12pt,headsepline]{report}
\geometry{left=2cm, right=2cm, top=2.5cm, bottom=2cm}
\linespread {1.25}\selectfont
\usepackage[nottoc]{tocbibind}
\frenchspacing
\usepackage{tabularx}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{geometry}

\begin{document}

    %...
    % need table here
    % ...

\end{document}
  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Jun 12 '14 at 10:15
  • Now you have enough reputation for voting answers :) BTW: welcome to TeX.SX! – Claudio Fiandrino Jun 12 '14 at 13:22
4
\documentclass{report}
\usepackage{ragged2e,array}
\newcommand\MC[1]{\multicolumn{2}{|c}{%
  \tabular{@{\quad} >{\Centering}p{\dimexpr2.5cm} @{\quad}}
    CPU targets,\\byte offset #1\endtabular}}
\begin{document}

\begin{tabular}{*4{| p{1.25cm} | >{\RaggedLeft}p{1.25cm} }| }
31 & 24 & 23 & 16 & 15 & 8 & 7 & 0\\\hline
\MC3 & \MC2 & \MC1 & \MC0 \vrule\\\hline
\end{tabular}

\end{document}

enter image description here

  • awesome code, thanks! Cant vote up your answer, have too low reputation, sorry, can only select as accepted answer – user2950911 Jun 12 '14 at 10:46
5

With bytefield help you can easily get

\documentclass{article}
\usepackage{bytefield}

\begin{document}
\begin{bytefield}[bitheight=8ex,endianness=big]{32}
    \bitheader{31,24,23,16,15,8,7,0}\\
    \bitbox{8}{CPU targets,\\ byte offset 3}
    \bitbox{8}{CPU targets,\\ byte offset 2}
    \bitbox{8}{CPU targets,\\ byte offset 1}
    \bitbox{8}{CPU targets,\\ byte offset 0}
\end{bytefield}
\end{document}

enter image description here

  • this is also a very helpful answer, thanks! I would vote your answer up, but I cant, for having too low reputation, sorry – user2950911 Jun 12 '14 at 10:50
3

Here's an implementation with low level commands:

\documentclass{article}

\makeatletter
\newcommand{\bitstable}[5][\columnwidth]{%
  $\m@th\vcenter{\vbox{\bits@table{#1}{#2}{#3}{#4}{#5}}}$%
}

\newcommand\bits@table[5]{%
  \offinterlineskip
  \@tempdima=#1\relax
  \advance\@tempdima-4\p@
  \@tempdima=.25\@tempdima
  \tabskip=\z@
  \halign{%
    \vrule##\hfil&
    ##&
    \hfil\vrule##\hfil&
    ##&
    \hfil\vrule##\hfil&
    ##&
    \hfil\vrule##\hfil&
    ##&
    \hfil\vrule##\cr
    &
    \footnotesize\rlap{\,31}\hfil\vrule\hfil\llap{24\,}&
    &
    \footnotesize\strut\rlap{\,23}\hfil\vrule\hfil\llap{16\,}&
    &
    \footnotesize\strut\rlap{\,15}\hfil\vrule\hfil\llap{8\,}&
    &
    \footnotesize\strut\rlap{\,7}\hfil\vrule\hfil\llap{0\,}&
    \cr
    height 1pt&\hfil\vrule\hfil&&\hfil\vrule\hfil&&\hfil\vrule\hfil&&\hfil\vrule\hfil&\cr
    \noalign{\hrule height 0.8pt}
    width0.8pt height 3pt&&
    width0.8pt&&
    width0.8pt&&
    width0.8pt&&
    width0.8pt\cr
    width0.8pt&
    \bits@tablebox{#2}&
    width0.8pt&
    \bits@tablebox{#3}&
    width0.8pt&
    \bits@tablebox{#4}&
    width0.8pt&
    \bits@tablebox{#5}&
    width0.8pt\cr
    width0.8pt height 3pt&&
    width0.8pt&&
    width0.8pt&&
    width0.8pt&&
    width0.8pt\cr
    \noalign{\hrule height 0.8pt}
  }%
}
\newcommand{\bits@tablebox}[1]{%
  \parbox{\@tempdima}{\centering\strut#1\strut}%
}
\makeatother

\begin{document}

\noindent
\bitstable
  {CPU targets\\ byte offset $3$}
  {CPU targets\\ byte offset $2$}
  {CPU targets\\ byte offset $1$}
  {CPU targets\\ byte offset $0$}

\bigskip

\noindent
\bitstable[.8\linewidth]
  {CPU targets\\ byte offset $3$}
  {CPU targets\\ byte offset $2$}
  {CPU targets\\ byte offset $1$}
  {CPU targets\\ byte offset $0$}

\end{document}

enter image description here

1

Here, I introduce \boxit to make each individual box. It takes 3 arguments (the text, the upper bit, and the lower bit). It also has an optional argument of the box width, default 0.2\textwidth, so that 4 boxes take up 0.8\textwidth.

\documentclass{article}
\usepackage{lipsum}
\usepackage[usestackEOL]{stackengine}
\def\mystrut{\rule[-2pt]{\fboxrule}{2ex}}
\newcommand\boxit[4][.2\textwidth]{%
  \leavevmode\unskip%
  {\small\sffamily%
  \fboxsep=0pt%
  \def\stacktype{L}%
  \strutlongstacks{T}%
  \def\stackalignment{r}%
  \stackon{\def\stackalignment{l}%
  \stackon{\def\stackalignment{c}%
  \stackon{\fbox{\addstackgap[1pt]{\makebox[#1]{\Longunderstack{#2}}}}}{\mystrut}%
  }{\mystrut\,#3}%
  }{#4\,\mystrut}}%
  \kern-.5\fboxrule\ignorespaces%
}
\begin{document}
\lipsum[1]
\begin{center}
\boxit{CPU targets,\\byte offset 3}{31}{24}
\boxit{CPU targets,\\byte offset 2}{23}{16}
\boxit{CPU targets,\\byte offset 1}{15}{8}
\boxit{CPU targets,\\byte offset 0}{7}{0}
\end{center}
\lipsum[2]
\end{document}

enter image description here

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.