在长表中嵌入索引或词汇表,并在参数中自动执行超链接和分隔符

在长表中嵌入索引或词汇表,并在参数中自动执行超链接和分隔符

我正在寻找制作像这样的表格:

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

...通过使用makeindex,我想探索实现这一点的可行性。

思路如下:

在主文档中使用时\index[table]{#1}{#2}{#3}{#4},我想通过常规使用\printindex[table](或任何其他可以想出的索引名称)在文档的其他位置生成一个长表。

综合请求概述如下:

  1. \index 命令本身在该位置创建一个\hyperlink(嵌入在 a 中\marginpar)和,参见第 5 点。\hypertarget

  2. #1是生成列“语法”的 (ttstyle) 代码。对 中的代码进行两件事#1:(i) 步骤 (ia) 是确定是否存在分隔符(。(ib) 如果有分隔符,则使用分隔符之前的所有文本字符(在列“R 代码”中重复使用。(ic) 如果没有这样的分隔符,则引导编译器探测次要的分隔符,即,。第二个分隔符将区分逗号之前和之后,它将放置任何内容第一列中的逗号(“R 代码”)和“语法”中的逗号之后的任何内容。最好将 #1 内的任何内容嵌入到其他环境中,特别是listingsmintedtcolorbox。 (ii) 它允许使用第三分隔符,类似于\\将参数拆分为 longtable 中的单独行。longtable 本身中的代码(在此示例中dbinom)如下所示:

    & \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将产生第三列,该列具有相同的分隔符\\来分隔行(与 的行数相同#1)并产生与文档(正文)文本相同的字体和样式的文本。

  4. #3可以是 valueT或 value F。将布尔值设置为 true 将$\surd$在第四列中产生符号。

  5. #4除了第 1 点之外,还会做两件不同的事情:(i)它会生成指向发出 \index 命令的位置的链接。(ii)它会在第三列(顶部)的开头生成一个超目标(带有标签,#4它还将确定初始命令发出的初始标签),以正确链接到的这一部分。hypertargethyperlink\indexlongtable

现在,从中确定的参数数量#2将被重新使用,以发布该索引元素的适当布局。试图完全自动化这一过程(无限参数)确实要求太多了。我建议只检查数字并针对具体情况进行编码(例如\ifnumequal{1}{<layout for everything on one line>}\ifnumequal{2}{<\multirow{2}{*}{...} applied to column 1,4 and 5>}等)。这可能不是最好的解决方案,但我看不出有什么方法可以在没有一些认真计算的情况下实现自动化。

这里有一个问题:只有一个“元素”是不可取的(因此每个生成的元素\index 命令)打印在两页上。假设您使用两行(因为的\the\arguments#22,所以\multirow{2}{*}{<stuff>}),但页面上只剩下一行。因此,正确打印此内容需要提前查看确定的参数数量是否#2足以在该页面上打印,否则需要分页。

theindex必须更改此索引的环境以包含此 的标题,longtable并以 开头longtable。环境的结尾theindex必须包含问题。这可能可以使用钩子\end{longtable}来完成(?)etoolbox

下面是一段全面、解释性的代码。请注意,这是完全解释性的,因此不是完全最小化的(它是为了向目标展示所需的效果,但这个版本显然不是自动化的):

\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

我问这个问题已经很久了,我相信我的答案是对的。

基本思路如下,使用一个\rc包含三个参数的宏。第一个参数用于实际代码(index 生成的表中的第一列和第二列)。此代码嵌入在\mintinlineminted v2.0 alpha 3 中的新命令中,可下载这里)。第二个参数用于索引中的第三列,我用它来“解释”每一行。第三个参数将设置从文档中的点到附录/索引页的来回链接。(索引中的箭头将带您到您进行调用的点\rc,而将marginpar带您到索引。)

代码如下。索引样式和所有内容将自动生成,可独立运行*。


编辑如果您希望真正复制并粘贴下面的代码,您可能需要适合math.pyminted 的样式文件和词法分析器。我已经上传了样式这里和词法分析器这里

\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 和注释

\rc

  • 要使用宏(如上\code例所示),请改用\protect\code{...}
  • 里面的引号\code不起作用。请使用两个引号,例如\rc{read.table,""filename""}{...}{...}
  • 使用\code{"..."} 需要双引号或索引项不会出现.(我甚至不知道这是如何工作的,但我只想在这里提一下)
  • $...$目前不支持在宏中使用类似的数学环境\rc
  • 报告您发现的任何其他错误。

相关内容