2010-04-01 94 views
46

我想用一些迭代控制流來簡化下面的LaTeX代碼。乳膠中的迭代

\begin{sidewaystable} 
    \caption{A glance of images} 
    \centering 
    \begin{tabular}{| c ||c| c| c |c| c|| c |c| c|c|c| } 
    \hline 
    \backslashbox{Theme}{Class} &\multicolumn{5}{|c|}{Class 0} & \multicolumn{5}{|c|}{Class 1} \\ 
    \hline 

    \hline 
      1 & 
      \includegraphics[scale=2]{../../results/1/0_1.eps} 
      &\includegraphics[scale=2]{../../results/1/0_2.eps} 
      &\includegraphics[scale=2]{../../results/1/0_3.eps} 
      &\includegraphics[scale=2]{../../results/1/0_4.eps} 
      &\includegraphics[scale=2]{../../results/1/0_5.eps} 
      &\includegraphics[scale=2]{../../results/1/1_1.eps} 
      &\includegraphics[scale=2]{../../results/1/1_2.eps} 
      &\includegraphics[scale=2]{../../results/1/1_3.eps} 
      &\includegraphics[scale=2]{../../results/1/1_4.eps} 
      &\includegraphics[scale=2]{../../results/1/1_5.eps}  \\ 
    \hline 

    ... % similarly for 2, 3, ..., 22 

    \hline 
      23 & 
      \includegraphics[scale=2]{../../results/23/0_1.eps} 
      &\includegraphics[scale=2]{../../results/23/0_2.eps} 
      &\includegraphics[scale=2]{../../results/23/0_3.eps} 
      &\includegraphics[scale=2]{../../results/23/0_4.eps} 
      &\includegraphics[scale=2]{../../results/23/0_5.eps} 
      &\includegraphics[scale=2]{../../results/23/1_1.eps} 
      &\includegraphics[scale=2]{../../results/23/1_2.eps} 
      &\includegraphics[scale=2]{../../results/23/1_3.eps} 
      &\includegraphics[scale=2]{../../results/23/1_4.eps} 
      &\includegraphics[scale=2]{../../results/23/1_5.eps}  \\ 
    \hline 

    \end{tabular} 
    \end{sidewaystable} 

我得知forloop package提供for循環。但我不確定如何將其應用於我的案例?或者其他方法不通過forloop?


如果我也想簡單另一個類似的情況下,其中唯一的區別是該目錄不爲1,2運行,至23,但在一些任意的順序如3,2,6,9 ,...,甚至是一系列字符串,例如dira,dirc,dird,dirb,....我如何將LaTeX代碼轉換爲循環呢?

+1

請檢查是否接受的答案仍然是最好的選擇。最高票數的答案几乎是票數的三倍!謝謝。 – Sebastian 2013-11-08 15:48:36

回答

21

像這樣將您的表格的身體護理:

\newcounter{themenumber} 
\newcounter{classnumber} 
\newcounter{imagenumber} 
\forloop{themenumber}{1}{\value{themenumber} < 24}{ 
    % \hline <-- Error here 
    \arabic{themenumber} 
    \forloop{classnumber}{0}{\value{classnumber} < 2}{ 
     \forloop{imagenumber}{1}{\value{imagenumber} < 6}{ 
      & \includegraphics[scale=2]{ 
       ../../results/\arabic{themenumber}/\arabic{classnumber}_\arabic{imagenumber}.eps 
      } 
     } 
    } 
    \\ 
    \hline 
} 

我不得不註釋掉第一\hline,因爲它給了我一個錯誤:

You can't use `\hrule' here except with leaders. 

我不是確定這意味着什麼;如果你真的不能沒有雙線,我可以更多地考慮它。

另請注意,您必須使用<;例如,<= 24將不起作用。


至於你的更新:我只想宣佈需要,你遍歷參數的命令。事情是這樣的:

\newcommand\fordir[1]{do something complex involving directory named #1} 

\fordir{dira} 
\fordir{dirb} 
\fordir{dirc} 
\dots 
+0

謝謝,托馬斯!真的很感激它!是的,用雙水平線分隔行會很好。另一個問題:我怎樣才能得到分隔兩個多列的雙垂直線:\ multicolumn {5} {| c |} {Class 0}和\ multicolumn {5} {| c |} {Class 1}? – Tim 2010-04-01 20:42:17

98

您可以使用pgffor包,由PGF提供的一個工具。基本語法是:

\foreach \n in {0,...,22}{do something} 

值得注意的是,這對循環並不限於整數,例如:

\foreach \n in {apples,burgers,cake}{Let's eat \n.\par}