3

I'm looking to produce a table like this one:

http://i.imgur.com/oaCTJHf.png?1

... by the use of makeindex, and I'd like to explore the feasibility of achieving this.

The idea is as follows:

In the main document when using \index[table]{#1}{#2}{#3}{#4}, I'd like to produce a longtable elsewhere in the document, by the regular usage of \printindex[table] (or whatever other index name one can come up with).

The comprehensive request is outlined below:

  1. The \index command itself creates a \hyperlink (embedded in a \marginpar) and a \hypertarget at that location, see point 5.

  2. #1 is to produce the (ttstyle) code for the column "syntax". Two things are done with the code in #1: (i) step (i.a) is to determine if there is a delimiter (. (i.b) if there is a delimiter, use all text characters before the delimiter ( to be reused in the column "R code". (i.c) If there is no such delimiter, the compiler is then guided to probe a secondary delimiter, namely ,. The second delimiter will differentiate between before and after the comma which will place anything before the comma in the first column ("R code") and anything after in "syntax". Preferrably anything inside #1 could be embedded inside other environments, specifically listings, minted and tcolorbox. (ii) It would allow a tertiary delimiter, something similar to \\ to split up the arguments into seperate rows in the longtable. The code for this (in this example dbinom) in the longtable itself looks something like:

    & \texttt{\textcolor{blue}{dbinom(}\textcolor{cyan}{<getal>},} & \target{appendix.rcommand.dbinom}Kans op deze waarde \\
    \texttt{dbinom()} & \texttt{\textcolor{dgreen}{size=<waarde>},} & Bij zovaak proberen & $\surd$ & \hyperlink{document.rcommand.dbinom}{$\leftarrow$} \\
    & \texttt{\textcolor{red}{prob=<waarschijnlijkheid>}\textcolor{blue}{)}} & Kans op succes \\
    
  3. #2 would produce the third column, which would have the same delimiters \\ to seperate rows (same amount of rows as #1) and produce text in the same font and style as the document (body) text.

  4. #3 would be either value T or value F. Setting the boolean to true would produce the $\surd$ symbol in the fourth column.

  5. #4 would, besides point 1, do two seperate things: (i) It would produce the link to where the \index command was issued. (ii) it would produce a hypertarget (with the label of #4, which will also determine the labels of the initial hypertarget and hyperlink issued by the initial \index command) at the start of the third column (at the top) to correctly link to this part of the longtable.

Now, the amount of arguments that is determined from #2 will be reused to issue the appropriate layout for that index element. Trying to completely automate this (unlimited arguments) is truly too much asked for. I suggest just checking numbers and coding for a case by case scenario (e.g. \ifnumequal{1}{<layout for everything on one line>}, \ifnumequal{2}{<\multirow{2}{*}{...} applied to column 1,4 and 5>}, etc.). May not be the best solution but I don't see a way to automate this without some serious calculations.

There's a catch here: it's undesirable to have one "element" (so each produced element by one \index command) printed on two pages. Say, you use two rows (because the \the\arguments of #2 is 2, so \multirow{2}{*}{<stuff>}), but there's only one row left on the page. So printing this properly would require looking ahead to see if the amount of arguments determined from #2 is in fact sufficient to print on that page, and pagebreak otherwise.

The theindex environment for this index would have to be changed to include the header of this longtable, and start with the longtable. The end of the theindex environment would have to include the \end{longtable} issue. This can potentially be done using etoolbox hooks (?)

A comprehensive, explanatory piece of code is below. Please note this it entirely explanatory and therefore not completely minimal (it's to show to goal with the desired effect, but instead this version is obviously not automated):

\documentclass[10pt]{article}
\usepackage{xcolor}
\usepackage{multirow}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{ragged2e}
\usepackage{array}
\usepackage{hyperref}
\begin{document}
{\renewcommand\arraystretch{2.45}% arraystretch as argument of \printindex[table] or \makeindex[table]
\begin{longtable}[h]{>{\RaggedRight\arraybackslash}m{.175\linewidth} >{\RaggedRight\arraybackslash}m{.3\linewidth} >{\RaggedRight\arraybackslash}m{.25\linewidth} c l} %properly stretching to textwidth can be done internally
R code & Syntax & Gebruik & Vector? & Link
\\*\noalign{\vspace*{2.5ex}}\pagebreak[1] %same as head
\hline
\endhead
\texttt{[~]} & \texttt{\textcolor{cyan}{<matrix>}\textcolor{blue}{[}~,~,\textcolor{violet}{n}\textcolor{blue}{]}} & \hypertarget{appendix.rcommand.brace}{}Bij matrix van 3 dimensies, de/het \texttt{\textcolor{violet}{n}}'de rij/element in de \textbf{derde} dimensie weergeven (\textit{want: $3^e$ argument in de haakjes}), zie ook \hyperlink{appendix.rcommand.matrix}{$\rightarrow$}. & & \hyperlink{document.rcommand.matrix}{$\leftarrow$} \\
\texttt{?} & \texttt{?\textcolor{blue}{<commando>}} & \hypertarget{appendix.rcommand.question}{}Het opzoeken van R commando's && \hyperlink{document.rcommand.question}{$\leftarrow$} \\
\texttt{apropos()} & \texttt{\textcolor{blue}{apropos(}"\textcolor{cyan}{<commando>}"\textcolor{blue}{)}} & \hypertarget{appendix.rcommand.apropos}{}(Deel van een) commando opzoeken &  & \hyperlink{document.rcommand.apropos}{$\leftarrow$} \\
\texttt{as.table()} & \texttt{\textcolor{blue}{as.table(}\textcolor{cyan}{<array>}\textcolor{red}{)}} & \hypertarget{appendix.rcommand.as.table}{}R vertellen dat je de gegevens als een tabel wil lezen: read \texttt{as.table()}! & $\surd$ & \hyperlink{document.rcommand.as.table}{$\leftarrow$} \\
\multirow{3}{*}{\texttt{barplot()}} 
        & \texttt{\textcolor{blue}{barplot(}\textcolor{cyan}{<functie>},}
        & \label{anchor.appendix.barplot}\hypertarget{appendix.rcommand.barplot}{}Barplot maken (histogram, maar dan kolommen apart) \\     
    & \texttt{\textcolor{green}{col=c(}\textcolor{red}{"<kleur>",...}\textcolor{green}{)}\textcolor{blue}{)}}
    & \hypertarget{index.kleurspec}{}Verschillende kleuren\label{anchor.kleurspec} instellen met de \textbf{engelse} namen, bijv.: \texttt{"brown"} &  &  \\
\multirow{3}{*}{\texttt{binom.test()}} 
        & \texttt{\textcolor{blue}{binom.test(}\textcolor{cyan}{<tabel>},} 
        & \hypertarget{appendix.rcommand.binom.test}{}Gesorteerde data tabel onderwerpen aan binomiaal test & \multirow{3}{*}{$\surd$} & \multirow{3}{*}{\hyperlink{document.rcommand.binom.test}{$\leftarrow$}} \\
        & \texttt{\textcolor{green}{p=<waarde>}\textcolor{blue}{)}} 
        & Hypothetische kans op succes (nulhypothese) \\ 
\texttt{c()} & \texttt{\textcolor{blue}{c(}\textcolor{cyan}{<getal>},\textcolor{cyan}{ <getal>},\textcolor{red}{...}\textcolor{blue}{)}} & \hypertarget{appendix.rcommand.c}{}Handmatig een array maken. Voor langere getallen, gebruik \texttt{array()} & $\surd$ & \hyperlink{document.rcommand.c}{$\leftarrow$} \\
\texttt{colSums()} & {\texttt{\textcolor{blue}{colSums(}\textcolor{cyan}{<functie>}\textcolor{blue}{)}}} & Optellen van kolommen van een matrix. Voor rijen, gebruik: \hyperlink{appendix.rcommand.rowsums}{\texttt{rowSums()}$\rightarrow$} & $\surd$ &  \\ 
        & \texttt{\textcolor{blue}{dbinom(}\textcolor{cyan}{<getal>},} & \hypertarget{appendix.rcommand.dbinom}{}Kans op deze waarde \\
\texttt{dbinom()} & \texttt{\textcolor{green}{size=<waarde>},} & Bij zovaak proberen & $\surd$ & \hyperlink{document.rcommand.dbinom}{$\leftarrow$} \\
        & \texttt{\textcolor{red}{prob=<waarschijnlijkheid>}\textcolor{blue}{)}} & Kans op succes \\
\multirow{3}{*}{\texttt{expand.grid()}} & \texttt{\textcolor{blue}{expand.grid(}} & \hypertarget{appendix.rcommand.expand.grid}{}Aftellen meerdere dobbelstenen & & \multirow{3}{*}{\hyperlink{document.rcommand.expand.grid}{$\leftarrow$}} \\
        & \texttt{\textcolor{cyan}{<getal>}:\textcolor{green}{<getal>},\textcolor{red}{...}\textcolor{blue}{)}} & Minimale en maximale getal voor je `dobbelsteen' \\
\texttt{file.choose()} & \texttt{\textcolor{blue}{file.choose(}"\textcolor{cyan}{<bestand>}"\textcolor{blue}{)}} & \hypertarget{appendix.rcommand.file.choose}{}Bestanden laden in R && \hyperlink{document.rcommand.file.choose}{$\leftarrow$} \\
\texttt{help()} & \texttt{\textcolor{blue}{help(}\textcolor{cyan}{<commando>}\textcolor{blue}{)}} & \hypertarget{appendix.rcommand.help}{}Het opzoeken van R commando's && \hyperlink{page.4}{$\leftarrow$} \\
        & \texttt{\textcolor{blue}{hist(}\textcolor{cyan}{<functie>},} & \hypertarget{appendix.rcommand.hist}{}Histogram tekenen &  \\
\texttt{hist()} & \texttt{\textcolor{green}{breaks=}\textcolor{red}{<getal>},} & \hypertarget{appendix.rcommand.breaks}{}Breedte van de kolommen aangeven, zie \hyperlink{document.rcommand.breaks}{$\leftarrow$} && \hyperlink{document.rcommand.hist}{$\leftarrow$} \\
        & \texttt{\textcolor{violet}{freq=}\textcolor{purple}{<logische waarde>}\textcolor{blue}{)}} & \texttt{\textcolor{purple}{TRUE}}: plot de frequenties en \texttt{\textcolor{purple}{FALSE}}: plot verhouding tot geheel \\
\end{longtable}
}% end of index
\end{document}
  • 1
    Redefinition of theindex to use \begin{longtable} and \end{longtable} should pose no problem, since it is an environment and provides for begin and end code. – user31729 May 20 '14 at 20:14
  • @ChristianHupfer Even for split indexes? \AtBeginEnvironment{theindex[table]}{\begin{longtable}} + \AtEndEnvironment{theindex[table]}{\end{longtable}} didn't work for me? – 1010011010 May 20 '14 at 20:41
  • I really think this is too much for one question. Maybe you could break it down into discrete tasks and ask questions about those you can't achieve yourself? – cfr May 20 '14 at 23:08
  • 1
    Have you looked at glossaries? That has tabular options among the styles it offers, uses makeindex and manages hyperlinks. Perhaps you could adapt something there rather than trying to start from scratch? – cfr May 20 '14 at 23:12
  • @cfr Fair point. I've considered that but decided against it because so often, after I've provided too little contextual information, I was given an answer although pretty, not applicable to my situation. I guess asking the right questions is an art in itself. :-) – 1010011010 May 20 '14 at 23:12
0

It's been quite long since I asked this question and I believe I got it right.

The basic idea is as follows, use a macro \rc which takes up three arguments. First argument is for the actual code (first and second column in the table generated by index. This code is embedded in \mintinline, a command new in minted v2.0 alpha 3, which can be downloaded here). Second argument is for the third column in the index, which I use to "explain" each line. Third argument will set links back and forth from the point in the document to the appendix/index page. (The arrow in the index will take you to the point where you made the \rc call, while the marginpar will take you to the index.)

Code's below. Index styles and everything will be automatically generated, it works stand-alone*.


EDIT If you wish to truly copy and paste the code below, you may need the appropriate style file and math.py lexer for minted. I've uploaded the style here and the lexer here.

\documentclass{article}
\usepackage{longtable}
\usepackage{imakeidx}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{ragged2e}
\usepackage{xcolor}
\usepackage{array}
\usepackage{geometry}
\usepackage{minted}
\usepackage[T1]{fontenc}
\usepackage{filecontents}
\usepackage{kantlipsum}
\usepackage{hyperref}

\newcommand{\code}{\mintinline[style=euryris]{r}}

% some stuff for the index layout
\begin{filecontents}{index_rc.ist}
preamble "\\let\\HoldMyBeer\\hyperpage\n
\\renewcommand\\hyperpage\\mygobble\n
\\begin{theindex}\n\\noindent{\\renewcommand\\arraystretch{2.45}\\begin{longtable}[h]{>{\\RaggedRight\\arraybackslash}m{\\dimexpr.2\\linewidth-2\\tabcolsep} >{\\RaggedRight\\arraybackslash}m{\\dimexpr.3\\linewidth-2\\tabcolsep} >{\\RaggedRight\\arraybackslash}m{\\dimexpr.4\\linewidth-2\\tabcolsep} >{\\centering\\arraybackslash}m{\\dimexpr.1\\linewidth-2\\tabcolsep}} \\textbf{\\textcolor{blue}{R}} code & Syntax & Gebruik & Link
\\\\*\\noalign{\\vspace*{2.5ex}}\\pagebreak[1]
\\hline
\\endhead\\mygobble"
postamble "\n\\end{longtable}}\n\\end{theindex}\n\\let\\hyperpage\\HoldMyBeer\n"
item_0 "\\\\\n\\mymacro"
delim_0 ""
\end{filecontents}

\begin{filecontents}{index-begrip.ist}
preamble "\\let\\HoldMyBeer\\hyperpage\n
\\renewcommand\\hyperpage\\mygobble\n
\\begin{theindex}\n\\noindent{\\renewcommand\\arraystretch{2.7}\\begin{longtable}[h]{>{\\RaggedRight\\arraybackslash}m{\\dimexpr.225\\linewidth-2\\tabcolsep} >{\\RaggedRight\\arraybackslash}m{\\dimexpr.65\\linewidth-2\\tabcolsep} >{\\RaggedRight\\arraybackslash}m{\\dimexpr.1\\linewidth-2\\tabcolsep}} Begrip & Betekenis & Link
\\\\*\\noalign{\\vspace*{2.5ex}}\\pagebreak[1]
\\hline
\\endhead\\mygobble"
postamble "\n\\end{longtable}}\n\\end{theindex}\n\\let\\hyperpage\\HoldMyBeer\n"
item_0 "\\\\\n\\begripmacro"
delim_0 ""
\end{filecontents}

\begin{filecontents}{index_glossary.ist}
headings_flag 1
heading_prefix "{\\bfseries \\scshape "
heading_suffix "}\\nopagebreak\n"
symhead_positive "Symbolen"
symhead_negative "symbolen"
item_0 " \n  \\itshape\\item  "
item_1 " \n  \\itshape\\item  "
item_2 " \n  \\itshape\\item  "
delim_0 " \\dotfill "
delim_1 " \\dotfill "
delim_2 " \\dotfill "
\end{filecontents}

\makeindex[name = rc, title = \textbf{\textcolor{blue}{R}} commando's,options=-s index_rc,columns=1]
\makeindex[options= -s index_glossary]
\makeindex[name = begrip, title = Begrippenlijst,options=-s index-begrip,columns=1]

\makeatletter
\newcommand{\target}[1]{\Hy@raisedlink{\hypertarget{#1}{}\relax}} 
\makeatother 

\ExplSyntaxOn

\tl_new:N \g__ioiooiioio_link_name_tl

\NewDocumentCommand{\mymacro}{ m m m } 
 {
  \ioiooiioio_setargs:nnn { #1 } { #2 } { #3 }
  \ioiooiioio_print: 
 }

\seq_new:N \l__ioiooiioio_first_col_seq
\seq_new:N \l__ioiooiioio_second_col_seq
\tl_new:N \l__ioiooiioio_table_body_tl
\int_new:N \l__ioiooiioio_step_int
\int_new:N \l__ioiooiioio_args_int

\cs_new_protected:Npn \ioiooiioio_setargs:nnn #1 #2 #3
 {
  \seq_set_split:Nnn \l__ioiooiioio_first_col_seq { , } { #1 }
  \seq_set_split:Nnn \l__ioiooiioio_second_col_seq { , } { #2 }
  \tl_clear:N \l__ioiooiioio_table_body_tl
  \tl_gclear:N \g__ioiooiioio_link_name_tl
  \int_zero:N \l__ioiooiioio_step_int
  \int_zero:N \l__ioiooiioio_args_int
  \tl_gset:Nn \g__ioiooiioio_link_name_tl { #3 }
  \int_set:Nn \l__ioiooiioio_args_int { \seq_count:N \l__ioiooiioio_first_col_seq }
  \seq_mapthread_function:NNN
     \l__ioiooiioio_first_col_seq
     \l__ioiooiioio_second_col_seq
     \__ioiooiioio_fill_body:nn
 }

\cs_new_protected:Npn \__ioiooiioio_fill_body:nn #1 #2
  {
    \int_incr:N \l__ioiooiioio_step_int
    \int_compare:nTF { \l__ioiooiioio_step_int = \c_one }
  {    
    \tl_put_right:Nn \l__ioiooiioio_table_body_tl { 
    & \target{ appendix.rc. \g__ioiooiioio_link_name_tl }\code{#1(} & #2 & 
    }
   }   
    {
   \int_compare:nTF { \l__ioiooiioio_step_int = \l__ioiooiioio_args_int }
   {
    \tl_put_right:Nn \l__ioiooiioio_table_body_tl { 
    & \code{#1)} & #2 & 
    }
   }
   {
    \tl_put_right:Nn \l__ioiooiioio_table_body_tl { 
    & \code{#1} & #2 & }
   }
   } 
    \int_compare:nT { \l__ioiooiioio_step_int = \c_one }
  {
     \tl_put_right:Nx \l__ioiooiioio_table_body_tl
       {
         \exp_not:N \multirow { \int_use:N \l__ioiooiioio_args_int } { * } 
           {
             \exp_not:N \hyperlink 
               { doc.rc.\g__ioiooiioio_link_name_tl } 
               { $ \leftarrow $ }
           }
       }
  }
     \int_compare:nTF { \l__ioiooiioio_step_int = \l__ioiooiioio_args_int} 
       {
         \tl_put_right:Nn \l__ioiooiioio_table_body_tl
           { \\ } 
       }
       {
       \tl_put_right:Nn \l__ioiooiioio_table_body_tl
           { \\* }
       }
   }

\cs_new_protected:Npn \ioiooiioio_print:
 {
 \multirow{ \int_use:N \l__ioiooiioio_args_int }{*}
 { 
\begingroup\edef\code{\endgroup\noexpand\mintinline[style=euryris]{r}{\seq_item:Nn \l__ioiooiioio_first_col_seq { \c_one }()}}\code 
 } 
 \l__ioiooiioio_table_body_tl
   }

\NewDocumentCommand{\mymargin}{ m } 
 {
  \ioiooiooio_determine_args:n { #1 } 
  \ioiooiooio_print: 
 }

\seq_new:N \l__ioiooiooio_arguments_seq
\tl_new:N \l__ioiooiooio_mapped_args_tl

\cs_new_protected:Npn \ioiooiooio_determine_args:n #1 
 {
  \seq_set_split:Nnn \l__ioiooiooio_arguments_seq { , } { #1 }
  \tl_clear:N \l__ioiooiooio_mapped_args_tl
  \seq_map_function:NN
     \l__ioiooiooio_arguments_seq
     \__ioiooiooio_mapped_args:nn
 }

\cs_new_protected:Npn \__ioiooiooio_mapped_args:nn #1 
  {
    \tl_put_right:Nn \l__ioiooiooio_mapped_args_tl { #1 }
   }

\cs_new_protected:Npn \ioiooiooio_print:
 {
\begingroup\edef\code{\endgroup\noexpand\mintinline[style=euryris]{r}{\seq_item:Nn \l__ioiooiooio_arguments_seq { \c_one }()}}\code 
 } 

\ExplSyntaxOff
\newcommand{\mygobble}[1]{}%
\newcommand{\marginbox}[2]{\marginpar[]{\resizebox{\ifdim\width>\marginparwidth\marginparwidth\else\width\fi}{!}{\hyperlink{#1}{#2\marginformat}}}}
\newcommand{\marginformat}{~$\rightarrow$}
\newcommand{\rc}[3]{
\index[rc]{{#1}{#2}{#3}}
\target{doc.rc.#3}
\marginbox{appendix.rc.#3}{\mymargin{#1}}
}

\newcommand{\begripmacro}[3]{#1&\target{appendix.begrip.#3}#2&\hyperlink{doc.begrip.#3}{$\leftarrow$}}
\newcommand{\begrip}[3]{
\index[begrip]{{#1}{#2}{#3}}
\target{doc.begrip.#3}
\marginbox{appendix.begrip.#3}{\textit{#1}}
}

\begin{document}

\code{read.table()} % mintinline works fine before using index

abc

some more indices\index{index}\index{test}

Really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really long line.\begrip{een}{begrip}{longline}

\kant[1-3]A really long sentence involving nothing related Kant, but is fully explanatory.\rc{read.table,een,twee,drie}{stuff,meer,stuff,stuff}{stuff} \\ \kant[4]

\printindex[rc] 

\printindex[begrip] % define an arbitrary glossary style
\printindex % other indexes work just fine

\code{read.table()} % mintinline also works fine after using index

\end{document}

BUGLIST and NOTES

\rc:

  • To use macros (like \code in the example above), use \protect\code{...} instead.
  • Quotation marks inside \code will not function. Use two quotation marks instead, e.g. \rc{read.table,""filename""}{...}{...}.
  • Usage of \code{"..."} also requires double quotation marks or the index item will not appear. (Don't even know how that works but I'll just mention it here)
  • Math environments like $...$ are currently not supported to be used inside the macro \rc.
  • Report any additional bugs that you may find.

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.