15

Im wondering if I can do some kind of table in LaTeX in order to show my C array nicely in text? I did something like this in KolourPaint:

enter image description here

Tried to do the same with LaTeX tables, but with no success ... Is it even possible?

I have only something like this so far (not very impressive, I know):

\documentclass[11pt]{article}
\begin{document}

\begin{table}[ht]
\begin{center}
\begin{tabular}{|c|c|}
    \multicolumn{2}{c}{tab[0]  tab[1]}\\ \hline
    123 & 23 \\ \hline
\end{tabular}
\end{center}
\label{tab:multicol}
\end{table}

\end{document}
10
  • 2
    What have you tried so far? Please share with us the table that proves "unsuccessful" with \documentclass and everything :)
    – nickpapior
    Apr 3, 2014 at 16:40
  • @zeroth: ok, will add the example but its not much of it :( Apr 3, 2014 at 16:40
  • @BrianBrown Why the \multicolumn?
    – Manuel
    Apr 3, 2014 at 16:56
  • @Manuel I'd guess to remove the vertical rules, but this is never going to be lined up correctly in a clean way.
    – hugovdberg
    Apr 3, 2014 at 16:59
  • @hugovdberg Okey, didn't imagine that.
    – Manuel
    Apr 3, 2014 at 17:01

2 Answers 2

15

This is far from okey. But it works. I hope someone comes with a more automatized version (I would even prefer TikZ for this over my solution). EDIT I was silly enough to make first and last line with \multicolumn while it's easier to use it just in the middle row. I keep this unchanged, since I'm not fond (:P) of this solution.

\documentclass{scrartcl}

\begin{document}
\begin{tabular}{|c|c|c|}
    \multicolumn{1}{c}{a} & \multicolumn{1}{c}{b} & \multicolumn{1}{c}{c} \\ \hline
    c & d & e \\ \hline
    \multicolumn{1}{c}{e} & \multicolumn{1}{c}{f} & \multicolumn{1}{c}{g}
\end{tabular}
\end{document}

enter image description here


And here goes something to play with tap, which can break some things as it mixes Plain Tex with Latex, but… I like the output (I also used it here).

\documentclass{scrartcl}
\input{tap}

\begin{document}
\begintable
    \begintableformat
        & \center
    \endtableformat
    \B": \texttt{tab[0]} " \texttt{tab[1]} " \texttt{tab[2]} " \texttt{tab[3]} " \texttt{tab[4]} " \texttt{tab[5]} " \texttt{tab[6]} " \texttt{tab[7]} \E"
    \=
    \B!: 1234 ! 56 ! 1212 ! 33 ! 1434 ! 80 ! 1312 ! 78 \E!
    \=
    \B": 65508 " 65510 " 65512 " 65514 " 65516 " 65518 " 65520 " 65522 \E"
\endtable
\end{document}

enter image description here

4
  • No, no, don't accept this. I think someone will come up with a better, cleaner, and more automatized solution.
    – Manuel
    Apr 3, 2014 at 17:47
  • 2
    Just rewrite the \mutlicolumn commands so you can just do something like \mcs{<text>} instead of \multicolumn{1}{c}{\textsf{#1}} every time... (Never heard of tap before!)
    – jon
    Apr 3, 2014 at 17:51
  • @jon That's true, but I meant something like Sean Allred solution or even something like \begin{Carray} a & b & c \\ c & d & e \\ e & f & g \end{Carray} to get the exact ouput as mine. EDIT: About tap I like its flexibility, I hope someone with the necessary skills translates it to “Latex”. It's similar to the Context approach, I think.
    – Manuel
    Apr 3, 2014 at 17:59
  • Well, it's true that I'd use multicolumn on the 'box' line (as Sean's answer does); but LaTeX does not have good 'row' support by default. ConTeXt would handle this problem much more easily with the \setupTABLE command (forget the exact name).
    – jon
    Apr 3, 2014 at 17:59
16

Consider using something plainer, like this:

enter image description here

\documentclass{article}
\usepackage{expl3,xparse}
\ExplSyntaxOn
\keys_define:nn { brian / c-array } {
  name         .tl_set:N = \l_brian_c_array_name_tl,
  name-fmt     .tl_set:N = \l_brian_c_array_name_fmt_tl,
  index-start .int_set:N = \l_brian_c_array_index_int,
  addr-start  .int_set:N = \l_brian_c_array_addr_int,
  addr-step   .int_set:N = \l_brian_c_array_addr_step_int,
  addr-fmt     .tl_set:N = \l_brian_c_array_addr_fmt_tl,
  inner-fmt    .tl_set:N = \l_brian_c_array_inner_fmt_tl,
  top-skip    .dim_set:N = \l_brian_c_array_top_skip_dim,
  bottom-skip .dim_set:N = \l_brian_c_array_bottom_skip_dim,
  inner-strut  .tl_set:N = \l_brian_c_array_inner_strut_tl,
}

\cs_new:Nn \__brian_temp:n{
  & \multicolumn{1}{c|}{
    \l_brian_c_array_inner_fmt_tl
    #1
  }
}

\NewDocumentCommand \CArray { O{} m O{\rule[-2.25ex]{0pt}{6ex}}} {
  \group_begin:
  \keys_set:nn
    { brian / c-array }
    { name=arr,
      name-fmt=\ttfamily\small,
      index-start=0,
      addr-start=2000,
      addr-step=4,
      top-skip = \medskipamount,
      bottom-skip = 3ex,
      inner-strut = \rule[-2.25ex]{0pt}{6ex},
      inner-fmt = \rmfamily,
      addr-fmt = \rmfamily,
      #1 }

  \seq_set_split:Nnn \l_tmpa_seq { , } { #2 }
  \seq_pop_left:NN   \l_tmpa_seq \l_tmpa_tl
  \int_set:Nn \l_tmpa_int { \seq_count:N \l_tmpa_seq }


  \begin{tabular}{*{\int_eval:n { \seq_count:N \l_tmpa_seq + 1 }}{c}}
    \tl_use:N \l_brian_c_array_name_fmt_tl
    \tl_use:N \l_brian_c_array_name_tl
    [\int_use:N \l_brian_c_array_index_int]
    \prg_replicate:nn {\int_use:N \l_tmpa_int}
      { 
        &
        \int_gincr:N \l_brian_c_array_index_int
        \tl_use:N \l_brian_c_array_name_fmt_tl
        \tl_use:N \l_brian_c_array_name_tl
        [\int_use:N \l_brian_c_array_index_int]
      }
      \\[\l_brian_c_array_top_skip_dim]
      \hline

      \multicolumn{1}{|c|}{
        \l_brian_c_array_inner_strut_tl
        \l_brian_c_array_inner_fmt_tl
        \tl_use:N \l_tmpa_tl
      }
      \seq_map_function:NN \l_tmpa_seq \__brian_temp:n
      \\
      \hline
      \rule{0pt}{\l_brian_c_array_bottom_skip_dim}
      \l_brian_c_array_addr_fmt_tl
      \int_use:N \l_brian_c_array_addr_int
      \prg_replicate:nn { \int_use:N \l_tmpa_int }
        {
          & \int_gadd:Nn \l_brian_c_array_addr_int
              {
                \int_use:N \l_brian_c_array_addr_step_int
              }
            \l_brian_c_array_addr_fmt_tl
            \int_use:N \l_brian_c_array_addr_int
        }
  \end{tabular}

  \group_end:
}
\ExplSyntaxOff

\begin{document}
\centering

\CArray[
addr-start=65508,
addr-step=4,
name=tab,
name-fmt=\ttfamily\small,
inner-fmt=\ttfamily,
]{1234, 56, 1212, 33, 1434, 80, 1312, 78}

\vspace{1in}

\CArray[inner-fmt=\ttfamily\large,name-fmt=\ttfamily]
{1,2,3}
\end{document}

If you really want the tabular shadow, I can edit that in. (I need to figure out how to do it, first :))

A big thanks to @hugovdberg and @egreg for sorting out the intermediate snafoo with \seq_map_…:. :)

2
  • nice! its very kind of you, but dont bother yourself with the shadow ;) I can live without it :P cheers! Apr 3, 2014 at 17:45
  • @BrianBrown See my edit :) And for shadows, you might want to check out the fancybox package. I'm not sure how useful it could be in this implementation, but it's worth a look. (At the very least, you can wrap the whole thing in a \shadowbox: i.stack.imgur.com/Qu51O.png) Apr 3, 2014 at 22:48

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.