# Using patterns to modify TeXForm output

When I use TeXForm on a matrix I often want to replace some the output with some number of verticle and horizontal lines. Is there a way to get control over this, possibly using patterns?

As an example consider

TeXForm[TableForm[{{1, 2}, {3, 4}}, TableHeadings -> {{"a", "b"}, {"c", "d"}}]]
(*Output:
\begin{array}{ccc}
& \text{c} & \text{d} \\
\text{a} & 1 & 2 \\
\text{b} & 3 & 4 \\
\end{array}
*)


I would like the output,

\begin{array}{c|c|c}
& \text{c} & \text{d} \\ \hline
\text{a} & 1 & 2 \\
\text{b} & 3 & 4 \\
\end{array}


Is there a way this can be automatically accomplished?

I think it would be better in the long run if you simply replaced TableForm by Grid before converting to TeXForm. Unlike TableForm, Grid dividers are translated correctly into $\LaTeX$:

TeXForm[
Grid[
Prepend[Transpose[
Prepend[Transpose[
{{1, 2}, {3, 4}}
],
{"a", "b"}]],
{"", "c", "d"}],
Dividers -> {{None, Automatic, Automatic}, {None, Automatic,
None}}]
]

(* =>
//TeXForm=
\begin{array}{c|c|c}
\text{} & \text{c} & \text{d} \\
\hline
\text{a} & 1 & 2 \\
\text{b} & 3 & 4 \\
\end{array}
*)


$$\begin{array}{c|c|c} \text{} & \text{c} & \text{d} \\ \hline \text{a} & 1 & 2 \\ \text{b} & 3 & 4 \\ \end{array}$$