# Make an C programming language array in LaTeX

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:

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}

-
What have you tried so far? Please share with us the table that proves "unsuccessful" with \documentclass and everything :) –  zeroth Apr 3 '14 at 16:40
@zeroth: ok, will add the example but its not much of it :( –  Brian Brown Apr 3 '14 at 16:40
@BrianBrown Why the \multicolumn? –  Manuel Apr 3 '14 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 '14 at 16:59
@hugovdberg Okey, didn't imagine that. –  Manuel Apr 3 '14 at 17:01

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}


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}


-
whoa! Its really beautiful, thank you! –  Brian Brown Apr 3 '14 at 17:44
No, no, don't accept this. I think someone will come up with a better, cleaner, and more automatized solution. –  Manuel Apr 3 '14 at 17:47
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 '14 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 '14 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 '14 at 17:59

Consider using something plainer, like this:

\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,
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,
top-skip = \medskipamount,
bottom-skip = 3ex,
inner-strut = \rule[-2.25ex]{0pt}{6ex},
inner-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}
\prg_replicate:nn { \int_use:N \l_tmpa_int }
{
{
}
}
\end{tabular}

\group_end:
}
\ExplSyntaxOff

\begin{document}
\centering

\CArray[
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_…:. :)

-
nice! its very kind of you, but dont bother yourself with the shadow ;) I can live without it :P cheers! –  Brian Brown Apr 3 '14 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) –  Sean Allred Apr 3 '14 at 22:48