4

I currently have a table using pgfplotstable that looks as follows. enter image description here

I would like to add text to one of these cells such that it does not move the position of the table in the page and does not change the alignment of the text in the column. I made a little mock-up of what I'd like. I think of this as sort of adding a label to a column. enter image description here

This is what I have that generates the table so far. I also have the data file, included in the code by using filecontents.

\documentclass{article}

\usepackage{amssymb, amsmath, amsthm}
\usepackage[margin=1in]{geometry}
\usepackage{fancyhdr}
\usepackage{appendix}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.15}
\usepackage{booktabs}
\usepackage{multicol}
\usepackage{filecontents}
\begin{filecontents}{lmax.dat}
wavelength      T       abs
400     70.4    0.152
410     63.9    0.194
420     57.8    0.238
430     52.9    0.277
440     49.5    0.305
450     47.4    0.324
460     46.9    0.329
470     47.8    0.321
480     49.9    0.302
490     53.1    0.275
500     57.3    0.242
510     62.2    0.206
520     67.2    0.173
530     72.3    0.141
540     78.3    0.106
550     82.8    0.082

\end{filecontents}

\pagestyle{fancy}


\begin{document}
\begin{table}[!htbp]
        \centering
        \caption{Absorbance and transmittance for solution E at various wavelengths}
        \label{tab:lmax}
        \vspace{1em}
        \pgfplotstabletypeset[
            every head row/.style={before row=\toprule, after row=\midrule},
            every last row/.style={after row=\bottomrule},
            columns/wavelength/.style={column name={$\lambda$ (nm)}},
            columns/T/.style={fixed, fixed zerofill, precision=1, column name={$\%T$}},
            columns/abs/.style={fixed, fixed zerofill, precision=3, column name={$A=-\log(T)$}}
        ]{lmax.dat}
    \end{table}
\end{document}
koleygr
  • 20,105
  • Welcome! I used the freedom to show you how we include files in our code to make easier the life of them who want to answer. But your code needs to forced compile to give the output. I olny added filecontents (with your file) and the command \pgfplotsset{compat=1.15}. Please try it and see what part we are missing... – koleygr Sep 17 '18 at 06:08
  • I don't suppose it's a option to treat the third column as a string and just add \rlap{\space$(\lambda_\text{max})$} to your data file? – David Purton Sep 17 '18 at 14:43

3 Answers3

2

To me this looks like a job for tikzmark. In this solution, you only need to specify the entry, i.e. row and column you want to mark. All I do is to tell pgfplotstable to append a tikzmark to the appropriate entry, and then let tikzmark do its magic.

\documentclass{article}
\usepackage{amssymb, amsmath, amsthm}
\usepackage[margin=1in]{geometry}
\usepackage{fancyhdr}
\usepackage{appendix}
\usepackage{pgfplots, pgfplotstable}
\usetikzlibrary{tikzmark}
\pgfplotsset{compat=1.15}
\usepackage{booktabs}
\usepackage{multicol}
\usepackage{filecontents}
\begin{filecontents}{lmax.dat}
wavelength      T       abs
400     70.4    0.152
410     63.9    0.194
420     57.8    0.238
430     52.9    0.277
440     49.5    0.305
450     47.4    0.324
460     46.9    0.329
470     47.8    0.321
480     49.9    0.302
490     53.1    0.275
500     57.3    0.242
510     62.2    0.206
520     67.2    0.173
530     72.3    0.141
540     78.3    0.106
550     82.8    0.082

\end{filecontents}

\pagestyle{fancy}


\begin{document}
\begin{table}[!htbp]
        \centering
        \caption{Absorbance and transmittance for solution E at various wavelengths}
        \label{tab:lmax}
        \vspace{1em}
        \pgfplotstabletypeset[
            every row 6 column 2/.style={postproc cell content/.style={@cell
            content=##1\tikzmark{x}}},
            every head row/.style={before row=\toprule, after row=\midrule},
            every last row/.style={after row=\bottomrule},
            columns/wavelength/.style={column name={$\lambda$ (nm)}},
            columns/T/.style={fixed, fixed zerofill, precision=1, column name={$\%T$}},
            columns/abs/.style={fixed, fixed zerofill, precision=3, column name={$A=-\log(T)$}}
        ]{lmax.dat}
    \end{table}
\begin{tikzpicture}[overlay,remember picture]
\node[anchor=west] at ([yshift=3pt]pic cs:x) {$(\lambda_\mathrm{max})$};
\end{tikzpicture}
\end{document}

enter image description here

The brand new version of the tikzmark library, which can be found here, makes this enterprise even more straightforward. Together with the positioning library it is really easy to put stuff just, say, 1mm right of a given entry. Zero manual adjustment needed. ;-)

\documentclass{article}
\usepackage{amssymb, amsmath, amsthm}
\usepackage[margin=1in]{geometry}
\usepackage{fancyhdr}
\usepackage{appendix}
\usepackage{pgfplots, pgfplotstable}
\usetikzlibrary{tikzmark,positioning}
\pgfplotsset{compat=1.15}
\usepackage{booktabs}
\usepackage{multicol}
\usepackage{filecontents}
\begin{filecontents}{lmax.dat}
wavelength      T       abs
400     70.4    0.152
410     63.9    0.194
420     57.8    0.238
430     52.9    0.277
440     49.5    0.305
450     47.4    0.324
460     46.9    0.329
470     47.8    0.321
480     49.9    0.302
490     53.1    0.275
500     57.3    0.242
510     62.2    0.206
520     67.2    0.173
530     72.3    0.141
540     78.3    0.106
550     82.8    0.082

\end{filecontents}

\pagestyle{fancy}


\begin{document}
\begin{table}[!htbp]
        \centering
        \caption{Absorbance and transmittance for solution E at various wavelengths}
        \label{tab:lmax}
        \vspace{1em}
        \pgfplotstabletypeset[
            every row 6 column 2/.style={postproc cell content/.style={@cell
            content=\tikzmarknode{x}{##1}}},
            every head row/.style={before row=\toprule, after row=\midrule},
            every last row/.style={after row=\bottomrule},
            columns/wavelength/.style={column name={$\lambda$ (nm)}},
            columns/T/.style={fixed, fixed zerofill, precision=1, column name={$\%T$}},
            columns/abs/.style={fixed, fixed zerofill, precision=3, column name={$A=-\log(T)$}}
        ]{lmax.dat}
    \end{table}
\begin{tikzpicture}[overlay,remember picture]
\node[right=1mm of x] {$(\lambda_\mathrm{max})$};
\end{tikzpicture}
\end{document}

enter image description here

  • Thank you! I like this solution because it keeps the anchor as relative to the actual cell as I can get. The only hard-coded moving is the yshift, which is minimal. Although, I noticed that if the tikzpicture is outside the table environment, it would draw the picture on the previous page. I just moved the tikzpicture into the table and it worked great. –  Sep 18 '18 at 03:33
1

An easy but hard-coded solution is to use tikz overlay and use manual positioning in according to current page dimensions:

\documentclass{article}

\usepackage{amssymb, amsmath, amsthm}
\usepackage[margin=1in]{geometry}
\usepackage{fancyhdr}
\usepackage{appendix}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.15}
\usepackage{booktabs}
\usepackage{multicol}
\usepackage{tikz}
\usepackage{filecontents}
\begin{filecontents}{lmax.dat}
wavelength      T       abs
400     70.4    0.152
410     63.9    0.194
420     57.8    0.238
430     52.9    0.277
440     49.5    0.305
450     47.4    0.324
460     46.9    0.329
470     47.8    0.321
480     49.9    0.302
490     53.1    0.275
500     57.3    0.242
510     62.2    0.206
520     67.2    0.173
530     72.3    0.141
540     78.3    0.106
550     82.8    0.082

\end{filecontents}

\newcommand{\tikzmark}[1]{\begin{tikzpicture}[overlay, remember picture]\coordinate (#1);\end{tikzpicture}}
\pagestyle{fancy}


\begin{document}
\begin{table}[!htbp]
        \centering
        \caption{Absorbance and transmittance for solution E at various wavelengths}
        \label{tab:lmax}
        \vspace{1em}
        \pgfplotstabletypeset[
            every head row/.style={before row=\toprule, after row=\midrule},
            every last row/.style={after row=\bottomrule},
            columns/wavelength/.style={column name={$\lambda$ (nm)}},
            columns/T/.style={fixed, fixed zerofill, precision=1, column name={$\%T$}},
            columns/abs/.style={fixed, fixed zerofill, precision=3, column name={$A=-\log(T)$}}
        ]{lmax.dat}
    \end{table}

    \begin{tikzpicture}[overlay, remember picture]
    \node at ([shift={(2.5,6.9)}]current page.center){$(\lambda_{max})$};
    \end{tikzpicture}
\end{document}

Of course this has to be done after the page has its final form and the numbers inside the shift command will be other numbers.

Output:

enter image description here

koleygr
  • 20,105
1

This solution doesn't require page coordinates, but it still requires computing offsets relative to the edges of the pgfplotstable (node).

While the extra text has no effect on the pgfplotstable, it does affect centering (slightly).

\documentclass{article}

\usepackage{amssymb, amsmath, amsthm}
\usepackage[margin=1in]{geometry}
\usepackage{fancyhdr}
\usepackage{appendix}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.15}
\usepackage{booktabs}
\usepackage{multicol}
%\usepackage{tikz}% already laoded
\usepackage{filecontents}
\begin{filecontents}{lmax.dat}
wavelength      T       abs
400     70.4    0.152
410     63.9    0.194
420     57.8    0.238
430     52.9    0.277
440     49.5    0.305
450     47.4    0.324
460     46.9    0.329
470     47.8    0.321
480     49.9    0.302
490     53.1    0.275
500     57.3    0.242
510     62.2    0.206
520     67.2    0.173
530     72.3    0.141
540     78.3    0.106
550     82.8    0.082

\end{filecontents}

\pagestyle{fancy}


\begin{document}
\begin{table}[!htbp]
        \centering
        \caption{Absorbance and transmittance for solution E at various wavelengths}
        \label{tab:lmax}
        \vspace{1em}
        \begin{tikzpicture}
        \node[inner sep=0pt] (A) {\pgfplotstabletypeset[
            every head row/.style={before row=\toprule, after row=\midrule},
            every last row/.style={after row=\bottomrule},
            columns/wavelength/.style={column name={$\lambda$ (nm)}},
            columns/T/.style={fixed, fixed zerofill, precision=1, column name={$\%T$}},
            columns/abs/.style={fixed, fixed zerofill, precision=3, column name={$A=-\log(T)$}}
        ]{lmax.dat}};
        \node[right, yshift={\dimexpr -9.4mm-5\baselineskip}, xshift=-8mm] at (A.north east) {$(\lambda_\text{max})$};
        \end{tikzpicture}
    \end{table}
\end{document}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120