9

All I am trying to do is write an integration by parts table in LaTeX. I can't seem to find an .tex code examples anywhere though I have seen pictures of such tables in PDF form (like at http://cazelais.disted.camosun.bc.ca/187/parts.pdf for example). Hopefully someone can let me know real quick.

3 Answers3

15

I assume you are asking about the tabular method of integration by parts, and one way would be to use tikzmark to note the location of the points and the after the table draw the arrows between the appropriate points:

enter image description here

Note:

  • This does require two runs. First one to determine the locations, and the second to do the drawing.

  • The \tikzmark is from Adding a large brace next to a body of text.

  • You can globally alter the style of the arrows via Arrow Style.

Code

\documentclass{article}

\usepackage{booktabs} \usepackage{xparse} \usepackage{tikz} \usetikzlibrary{calc}

\tikzset{Arrow Style/.style={text=black, font=\boldmath}}

\newcommand{\tikzmark}[1]{% \tikz[overlay, remember picture, baseline] \node (#1) {};% }

\newcommand{\XShift}{0.5em} \newcommand{\YShift}{0.5ex}

\NewDocumentCommand{\DrawArrow}{s O{} m m m}{% \begin{tikzpicture}[overlay,remember picture] \draw[->, thick, Arrow Style, #2] ($(#3.west)+(\XShift,\YShift)$) -- ($(#4.east)+(-\XShift,\YShift)$) node [midway,above] {#5}; \end{tikzpicture}% }

\begin{document} [ \renewcommand{\arraystretch}{1.5} \begin{array}{c @{\hspace*{1.0cm}} c}\toprule D & I \\cmidrule{1-2} x^2\tikzmark{Left 1} & \tikzmark{Right 1}e^2x \ 2x \tikzmark{Left 2} & \tikzmark{Right 2}\frac{1}{2} e^{2x} \
2 \tikzmark{Left 3} & \tikzmark{Right 3}\frac{1}{4} e^{2x} \
0 \tikzmark{Left 4} & \tikzmark{Right 4}\frac{1}{8} e^{2x} \\bottomrule \end{array} ] %----------------------------------------- \DrawArrow[draw=red]{Left 1}{Right 2}{$+$}% \DrawArrow[draw=brown]{Left 2}{Right 3}{$-$}% \DrawArrow[draw=blue]{Left 3}{Right 4}{$+$}% \end{document}

Peter Grill
  • 223,288
  • Awesome, that was what I was asking about. Is there a way to add labels above the arrows as well with tikzpicture? Perhaps with a way to specify where along the arrow the label will appear. – user25765 Feb 10 '13 at 22:47
  • @user25765: Sorry that I missed that earlier. Have updated the answer to include that. – Peter Grill Feb 10 '13 at 23:37
  • As I've gotten here via a recent link to this answer, I'd just like to note that tikzmark is now a tikz library and it is better to use the library version. – Andrew Stacey Jun 11 '21 at 15:56
6

Here's a start: There are various approaches to the arrows. I'll let someone else explain a tikz solution. But you can use pstricks and pst-nodes to accomplish it.

MWE:

\documentclass{article}
\usepackage{amsmath}
\usepackage{pstricks,pst-node}
\pagestyle{empty}
\newcommand{\diff}[1]{\mathrm{d}#1}
\newcommand{\me}{\mathrm{e}}
\begin{document}

\noindent Use \verb=\boxed{...}= to enclose the contents of an equation in a frame.
\[
    \boxed{\int_0^\infty \me^{-x^2}\diff{x}}
\]
Use \verb=\quad= and \verb=\text{...}= to add space and add regular
text in an equation
\[
    u = x \quad \text{ and } \quad \diff v = \cos x \diff x
\]
Use the \verb=align= environment to align math equation upon a given
position in the equation indicated by \verb=&=
\begin{align*}
    \int x \cos x \diff{x} &= x \sin x - \int \sin x \diff x \\
    &= x \sin x + \cos x +C
\end{align*}

\noindent\verb=pstricks= provides \verb=pst-node= to allow you to label and
connect nodes.
\renewcommand{\arraystretch}{3}
\[\begin{array}{c@{\hspace*{6em}}c}
    D                   & I                                  \\\hline
    x^2\rnode[br]{A1}{} & \me^{2ex}                          \\
    2x\rnode[br]{B1}{}  & \rnode[l]{B2}{}\frac{1}{2}\me^{2x} \\
    2\rnode[br]{C1}{}   & \rnode[l]{C2}{}\frac{1}{4}\me^{2x}
\end{array}\]
%%
\ncline[nodesep=1em,arrows=->]{A1}{B2}
\lput{:U}{\rput[c]{U}(0,2ex){line label}}
%%
\ncline[nodesep=1em,arrows=->]{B1}{C2}
\lput{:U}{\rput[c]{U}(0,2ex){line label}}


\end{document}

enter image description here

If you would like to go the pdflatex route using pstricks try the following MWE:

\documentclass{article}
\usepackage{amsmath}
\usepackage{pstricks}
\usepackage{auto-pst-pdf}
\usepackage{pst-node}
\pagestyle{empty}
\newcommand{\diff}[1]{\mathrm{d}#1}
\newcommand{\me}{\mathrm{e}}
\begin{document}

\noindent Use \verb=\boxed{...}= to enclose the contents of an equation in a frame.
\[
    \boxed{\int_0^\infty \me^{-x^2}\diff{x}}
\]
Use \verb=\quad= and \verb=\text{...}= to add space and add regular
text in an equation
\[
    u = x \quad \text{ and } \quad \diff v = \cos x \diff x
\]
Use the \verb=align= environment to align math equation upon a given
position in the equation indicated by \verb=&=
\begin{align*}
    \int x \cos x \diff{x} &= x \sin x - \int \sin x \diff x \\
    &= x \sin x + \cos x +C
\end{align*}

\noindent%
\begin{postscript}
\verb=pstricks= provides \verb=pst-node= to allow you to label and
connect nodes.
\renewcommand{\arraystretch}{3}
\[\begin{array}{c@{\hspace*{6em}}c}
    D                   & I                                  \\\hline
    x^2\rnode[br]{A1}{} & \me^{2ex}                          \\
    2x\rnode[br]{B1}{}  & \rnode[l]{B2}{}\frac{1}{2}\me^{2x} \\
    2\rnode[br]{C1}{}   & \rnode[l]{C2}{}\frac{1}{4}\me^{2x}
\end{array}\]
%%
\ncline[nodesep=1em,arrows=->]{A1}{B2}
\lput{:U}{\rput[c]{U}(0,2ex){line label}}
%%
\ncline[nodesep=1em,arrows=->]{B1}{C2}
\lput{:U}{\rput[c]{U}(0,2ex){line label}}
\end{postscript}

\end{document}

This is following the instructions found in the answer to How to use PSTricks in pdfLaTeX?. Also, using the environment \begin{postscript}...\end{postscript} is per the instructions found at the pstricks webside

Moriambar
  • 11,466
A.Ellett
  • 50,533
  • I would have liked this method for the fact that I can readily put labels but I have never been able to get pdflatex to work with PSTricks and I don't yet know how else to get Latex to present a printable form of my work. – user25765 Feb 10 '13 at 22:42
  • I've had problems using pdflatex, but you should be able write \usepackage[pdf]{pstricks} and the run pdflatex. My problems with pdflatex are very localized, but maybe you're running into similar headaches. I usually go the latex->dvips->ps2pdf route since it seems to better respect what I tell pstricks to do. – A.Ellett Feb 10 '13 at 22:44
  • from the command line try pdflatex -shell-escape <filehandle>. Make sure you've included \usepackage[pdf]{pstricks} and load pst-node through a second call to \usepackage{pst-node} – A.Ellett Feb 10 '13 at 22:51
3

With {NiceArray} of nicematrix.

\documentclass{article} 
\usepackage{nicematrix}
\usepackage{booktabs}
\usepackage{tikz}

\begin{document}

[ \renewcommand{\arraystretch}{1.5} \begin{NiceArray}{c @{\hspace*{1.0cm}} c}[create-medium-nodes] \toprule D & I \ \cmidrule{1-2} x^2 & e^2x \ 2x & \frac{1}{2} e^{2x} \
2 & \frac{1}{4} e^{2x} \
0 & \frac{1}{8} e^{2x} \ \bottomrule \CodeAfter \begin{tikzpicture} [->, name suffix = -medium] \draw [red] (2-1) -- node [above] {$+$} (3-2) ; \draw [brown] (3-1) -- node [above] {$-$} (4-2) ; \draw [blue] (4-1) -- node [above] {$+$} (5-2) ; \end{tikzpicture} \end{NiceArray} ]

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

F. Pantigny
  • 40,250