4

I'm working on an introductory course for the R-language in Latex, so I'm trying to write a Latex function that displays the R-function. This is what I've got:

\makeatletter  
\newcommand{\fun}[3]{  
    \begin{tcolorbox}[top = 0mm, bottom = 0mm, left = 0mm, right = 0mm, colback=green!5,colframe=vubgreen,title=#3]  
    \begin{tabular}{p{.2\textwidth}p{.04\textwidth}p{.68\textwidth}}  
    {\tt#1(}\\\@for\x:=#2\do{\x\\}{\tt$\quad$)}  
    \end{tabular}  
  \end{tcolorbox}}  
\makeatother

So, this function accepts three parameters: the name of the R-function, a list of R-parameters, and a description, with each R-parameter:

\renewcommand{\arg}[3][]{  
  {\tt$\quad$#2}&&#3}

So you can write for example:

\fun{hist}  
  {  
    \arg{x}{Vector.},  
    \arg{freq = TRUE}{Use absolute frequencies.}  
  }  
  {Draw a histogram.}

Now I'm having a few small problems to get it completely right: - The opening round bracket has to be on the same line as the first parameter (so the line under the first function. - The closing round bracket has to be right behind the last parameter (NOT behind the description of the parameter). - Behind each parameter there has to be a comma, except after the last one. - However: if a function has no parameters, opening and closing round bracket have to be on the same line as the name of the function.

Packages used (not all relevant for this example):

\usepackage{array}  
\usepackage{amsmath}  
\usepackage{enumerate}        
\usepackage{answers}      
\usepackage{graphicx}  
\usepackage{tcolorbox}  
\usepackage{color}  
\usepackage{xcolor}  
\usepackage{epstopdf}  
\usepackage[dutch]{babel}  
\usepackage{anysize}  
\usepackage[parfill]{parskip}  
\usepackage{hyperref}  
\usepackage{longtable}

I think this seems like a challenge, even for the TeXperts over here. All help is really appreciated!

4
  • If you have a block of code, you need to indent it by 4 spaces (or select it and click the Code Button {}) rather than add backticks before/after the code. Welcome! – Manuel Jan 23 '14 at 13:59
  • 3
    Writing about R in TeX you should seriously consider knitr (yihui.name/knitr), which allows you to write executable R in your document. That will guarantee the semantic correctness of your code as well as its proper prettyprinting. – Ethan Bolker Jan 23 '14 at 15:00
  • I've heard about knitr, but as everything is written in a different way I prefer not to switch. – m00se Jan 27 '14 at 12:17
  • @m00se "everything is written in a different way" What? Is just write LaTeX and just write R, nothing new except that you must say when R start with a line with <<>>= (optionally with many very useful parameters) and where R end (a line with only @) or alternatively, to just print the value or some R object of some function, you can use a \Sexpr{} like \Sexpr{round(log(pi),1)} (that will print "1.1") . There any examples of use of knitr in this site. You do not know what you will lose if you persist in your wrong belief ... – Fran Jan 30 '20 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.