1

I have a piece of Latex code that gets repeated at the start of every chapter of some lecture notes I'm working on.

\begin{flushleft}
\centering{\fbox{
\footnotesize
\begin{tabular}{lll}
Files: & Chapter 1 & \href{https://www.college.edu/files/file1_1.txt}{file1_1.txt} \\
& & \href{https://www.college.edu/files/file1_2.txt}{file1_2.txt} \\
& & \href{https://www.college.edu/files/file1_3.txt}{file1_3.txt}
\end{tabular} }}
\end{flushleft}

Is it possible create a macro that would save me having to use this template again and again? The only things that change each time I use it are the chapter number. and the list of links to files.

I would like to be able to call something like

ChapterMacro{Chapter 3}{file3_1, file3_2}

and have it generate the above Latex automatically. Can this be done?

2
  • Is the number of files fixed? Oct 19, 2016 at 13:10
  • 2
    unrelated to your question but \begin{flushleft}\centering{ is a very strange construct! \centering doesn't take an argument so the {} are not doing anything so it is the same as \begin{flushleft}\centering which is the same as \begin{center} in every way that I can think of, apart from looking rather obscure. Oct 19, 2016 at 13:46

2 Answers 2

1

enter image description here

\documentclass[10pt,a4paper]{report}
\usepackage{hyperref}
\makeatletter
\def\zzzz#1{%
\begin{center}\footnotesize
\fbox{ %
\protected\gdef\zztmp{Files&Chapter \thechapter&\gdef\zztmp{&&}}%
\gdef\zztmpb{}%
{\let\\\relax
\let\href\relax
\@for \f:=#1\do{\xdef\zztmpb{\zztmpb\zztmp
\href{https://www.college.edu/files/\f.txt}{\f.txt}\\}}}%
\begin{tabular}{lll}
\zztmpb
\end{tabular} %
}%
\end{center}}

\makeatother

\begin{document}

\chapter{zzz}

\zzzz{aaa,bbb}

\chapter{qqq}

\zzzz{xxx,yyy,zzz}

\end{document}
5
  • I did it as a table as that;' what you showed, although that makes the coding rather more complicated, but since there are no actual alignment points it would have been simpler just to set the href in a vertical list of paragraphs in a parbox. Oct 19, 2016 at 14:05
  • David, is this a bug? The above code does not work for file names with underscores, such as file_1 (even when escaped) Oct 19, 2016 at 14:14
  • Very nice thanks! I just noticed a problem though as the web address I am using has a tilde in it and this seems to mess things up. For example, can this macro will not work with: college.edu/~files/file1_1.txt - Is there a fix for this? Oct 19, 2016 at 14:51
  • And I should have said, I don't use the exact text 'Chapter 3'. It might be something like 'Chapter 3 - Implementation Rules' - I require the macro to handle dynamic text in the 'Chapter #' field just like it handles dynamic text for the file names. Oct 19, 2016 at 14:53
  • you can put \let~\relax just where I have \let\href\relax and then it'll be safe, as for the chapter, easy to give the command a second argument and then use #1 and #2` in place of the current \thechapter and #1 @eurocoder Oct 19, 2016 at 14:55
1

Using etoolbox, and as David suggested there's no point to have the flushleft and \centering, pick one. Also the chapter Counter is used, so the Argument {Chapter N} requested is optional.

\documentclass{book}
\usepackage{hyperref,etoolbox}

\makeatletter
\newcommand\beginchapter[2][Chapter \thechapter]{%
\fbox{Files from : #1
  \begin{tabular}[t]{l@{}}
    \forcsvlist{\make@beginchapter}{#2}
  \end{tabular}
}
}
\newcommand\make@beginchapter[1]{\href{https://www.college.edu/files/#1.txt}{#1.txt} \\}
\makeatother

\begin{document}
\chapter{Chap title}
\beginchapter{file1\_1, file1\_2, file1\_3}

\chapter{Chas}
\beginchapter[Some other text]{file2\_1, file2\_2, file2\_3}
\end{document}

Without optional argument

With optional argument

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .