Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6770131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:16:33+00:00 2026-05-26T15:16:33+00:00

I use latex to write up homework assignments. I need to include both charts

  • 0

I use latex to write up homework assignments. I need to include both charts and R code with my assignment. Sweave has worked well for me so far, but I’d like to simplify common code blocks like this…

\begin{rcode}
<<label=sol1, include=FALSE>>=
plot(c(2,3,5,7,11))
@
\end{rcode}
\begin{figure}[H]
<<fig=TRUE,echo=FALSE>>=
<<sol1>>
@
\end{figure}

(Where rcode is just a custom float…the code goes at end of document, the chart is stationary.)

So something like this…

\chart{sol1}{plot(c(2,3,4,5,7,11))}

where \chart is defined by

\newcommand{\chart}[2]{
  \begin{rcode}
  <<label=#1, include=FALSE>>=
  #2
  @
  \end{rcode}
  \begin{figure}[H]
  <<fig=TRUE,echo=FALSE>>=
  <<#1>>
  @
  \end{figure}
}

Sadly, it appears that sweave gets to the source code before latex processes \newcommand, and so this approach doesn’t work. Is there a way to tweak how the source is processed so that the macros are dealt with before sweave sees the source? Or is there a better approach to this kind of problem?

Thanks in advance for any suggestions..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:16:33+00:00Added an answer on May 26, 2026 at 3:16 pm

    I hope the following is about what you wanted to solve the hen/egg problem. I have stolen it from my own post on the R-list

    % ---------------------------------- 
    \documentclass{article} %
    \usepackage{Sweave} 
    \SweaveOpts{echo=FALSE}
    
    \newcommand\bloodp[3]{
      \subsection{Patient #1} For patient #1, the mean value of systolic pressure was #2~mmHg, the diastolic pressure was #3~mmHg. 
      \begin{figure}[!htb]% 
        \begin{center}% 
          \includegraphics{histo#1}%
          \caption{Histogram of systolic blood pressure for patient #1.}% 
          \label{fig:histo#1}% 
        \end{center}% 
      \end{figure}% 
      \clearpage % Better use FloatBarrier here 
    }
    
    \begin{document} 
    \section{Blood Pressure}
    
    <<results=tex>>= 
    n=100 
    dt = data.frame(
       subj=sample(1:3,n,TRUE), 
       syst=round(rnorm(n,120,10)),
       dia=round(rnorm(n,80,10))
       )
    # could also use tapply here 
    for (i in 1:3) { 
      dt1 = dt[dt$subj==i,] 
      cat("\\bloodp{",i,"}{", 
         round(mean(dt1$syst)),"}{", 
         round(mean(dt1$dia)),"}\n",sep="") 
       pdf(paste("histo",i,".pdf",sep="")) 
       hist(dt1$syst,main="",xlab="Blood pressure") 
       dev.off() } 
    @
    
    \end{document}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to use a font family in my LaTeX documents, that is available
I'm new to textmate and I want to use it to write latex. I
I just started to write a C program converting some LaTeX into HTML code.
I want to write a book on programming. I need to target both print
I am working on Sweave now, when I want to use R code to
I use emacs+AucTeX to write LaTeX files. At the bottom of the .tex file
I use latex to write papers and am often annoyed by the process of
I write code in isolation, that is I work for myself. I need some
Not sure this is a programming question, but we use LaTeX for all our
I want to prepare a text for the use in a LaTeX document. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.