4

I would like to repeat identical subsection format with different contents. For example

\documentclass{article}
\usepackage{graphicx}

\begin{document}

\section{Intro}

\subsection{A-text}

\begin{figure}[h]
\setkeys{Gin}{width=0.7\textwidth}
\includegraphics{figA}
\caption{This is A.}
\label{fig:001}
\end{figure}

\subsection{B-text}

\begin{figure}[h]    
\setkeys{Gin}{width=0.7\textwidth}
\includegraphics{figB}
\caption{This is B.}
\label{fig:002}
\end{figure}


\subsection{C-text}

\begin{figure}[h]
\setkeys{Gin}{width=0.7\textwidth}
\includegraphics{figC}
\caption{This is C.}
\label{fig:003}
\end{figure}


\end{document}

My idea is to build a new command using a control structure:

\documentclass{article}
\usepackage{graphicx}


\newcommand{\tempsect}[4]{
    \subsection{#1}

    \begin{figure}[h]
    \setkeys{Gin}{width=0.7\textwidth}
    \includegraphics{#2}
    \caption{fig:#3.}
    \label{fig:#4}
    \end{figure}

}

\begin{document}

\section{Intro}

for i in A to C {
% x1 of i
% x2 of i
% x3 of i
% x4 of i
\tempsect{x1,x2,x3,x4}
}

\end{document}

Eeach subsection A, B, C has different x1,x2,x3,x4 values. I have problems on several levels:

  1. how to write the correct command tempsect
  2. how to use which control structure?
  3. how to embed a look up table and using it (A->x1-x4, B->x1-x4)

I would be very happy if somebody could give me some hints how to solve this problem. If there is a solution using R/Sweave this would be also nice. Thanks for help.

Edit 03.09.2013/giordano:

  • to 2): I refer to the syntax of Latex control structure or loops.
  • to 3): For example, after first loop x1,x2,x4 and x4 contains "A-text","figA","This is A","001", respectively. The next loop the parameter x1,x2,x3, and x4 would contain "B-text","figB","This is B","002", respectively. Etc.
  • Could you explain a little bit more about 2) and 3)? \tempsect{x1,x2,x3,x4} is the wrong way to call the defined macro, use \tempsect{D-text}{D}{This is D}{004} instead. That does exactly what one would expect from the definition. Do you really want to loop over some sets of entries (that is not very easy in TeX)? What do you expect from that? – moewe Sep 2 '13 at 17:27
  • @moewe: Thanks for answer. I add some comments in my request regarding point 2) and 3). Yes, I want really to use loops. I have to write 30 subsection which identical structure but different figures, captions etc. This is only a minimal example. I didn't expected that it is easy but I thought if somebody who routinely use loops could provide the syntax and an example I could further develope the code and improve my latex know how. But maybe at the momenten due to my Latex unexperience I have to find another way, for example, kntir as Yihui suggest. – giordano Sep 3 '13 at 7:56
  • With bare (La)TeX, looping isn't that easy and convenient. Seeing that -- when you are looping -- you will at some point have to fill some sets (arrays) with data and retrieve the data afterwards, you might consider just using your command structure. Setting up and maintaining the loop (as well as arrays/sets of data etc.) will probably be less convenient than adding \tempsect{D}{D}{D}{004} to the document commands manually (if need be). – moewe Sep 3 '13 at 8:09
  • If you really insist on loops and such knitr or another higher-level solution might be more apt. You could also try and write a script (python/batch/bash) that does what you want. That way you'll have all the structures of normal programming languages at your disposal to create the LaTeX document you want. – moewe Sep 3 '13 at 8:14
  • @moewe: Thanks. It seems that I have to use knitr. – giordano Sep 3 '13 at 8:20
4

You can use knitr instead of Sweave, which allows you to program your documents. In your case, you just need a template, and knit() it as a child document in a for-loop. The example 020 in the knitr-examples repository shows you how.

|improve this answer|||||
  • thanks for this hint. I wanted to avoid knitr (not another tool!) but it seems that time arrived to learn knitr. – giordano Sep 3 '13 at 8:15
  • @giordano I understand you do not want to learn a new tool, but it is not essentially new. It just has much more power than Sweave. – Yihui Xie Sep 3 '13 at 16:35
  • Thanks for this information. I had a look at it and I will migrate to knitr. – giordano Sep 3 '13 at 18:31

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.