Generalincluding text from a "source" file

LaTeX specific issues not fitting into one of the other forums of this category.
dmt
Posts: 82
Joined: Sat Mar 15, 2008 8:13 pm

including text from a "source" file

Post by dmt »

I'm wondering if there is a package, or some easy way to do the following myself (I tried adapting the probsoln package, but it is a bit too specific for what I want).

I want to have some sort of source file with a punch of blocks of text, each with a label. Then I would like to be able to make a .tex file and just say to include some subset of these blocks of text (not plain text, I want to be able to include almost arbitrary sets of Latex commands in the blocks) just by listed up the label names.

In other words, I would like to be able to maintain a "master document" that I can easily take pieces of in whatever combination I want in new documents.

Is there something available to do this, or is it easy to do from scratch myself?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Re: including text from a "source" file

Post by phi »

Please be a bit more specific or give an example. How does your master file look like and how do you want to use it?
dmt
Posts: 82
Joined: Sat Mar 15, 2008 8:13 pm

Re: including text from a "source" file

Post by dmt »

The master doesn't have a set form, just different blocks of latex code I want to selectively include. Each block would have to have some kind of label to be able to choose it. So I could imagine something like this:

\begin{block}{label 1}
latex code goes here
\end{block}

\begin{block}{label 2}
latex code goes here
\end{block}

Then in my latex file I could do something like:

\includeblock{label 1}

And then that would be replaced on processing by everything in the label 1 block of code. I assume I would need to process several times to then process the new code that got added.

Something roughly like that.
dmt
Posts: 82
Joined: Sat Mar 15, 2008 8:13 pm

Re: including text from a "source" file

Post by dmt »

Much more specifically I was using the Exam class to write math tests. But I had to customize my files a bit to meet the format standards I needed. I then wanted to create a database of problems that I could just pick from to create new math tests by recombining them.

I thought I might be able to use the probsoln package to do this, but there seemed to be problems integrating the two in the way I need. I need to have the \question command associated with a particular problem in the database of problems since it includes special information about the number and type of points for the problem (there are different types of points, which is why I had to customize the files).

So I'm trying to figure out another way to do this. I figured there might be a very general way to do this, which might even be useful for other projects I may use in the future.
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

including text from a "source" file

Post by phi »

Hello,
try something like the following:

Code: Select all

\newsavebox\scratchbox
\newcommand*\includeblocklabel{}
\newcommand*\blocklabel{}
\newif\ifincludeblock

\newcommand*\includeblock[1]{%
  \renewcommand*\includeblocklabel{#1}%
  \input{masterfile.tex}%
}

\newenvironment{block}[1]{%
  \renewcommand*\blocklabel{#1}%
  \ifx\blocklabel\includeblocklabel
    \includeblocktrue
  \else
    \includeblockfalse
    \begin{lrbox}{\scratchbox}%
  \fi
}{%
  \ifincludeblock\else
    \end{lrbox}%
  \fi
}
dmt
Posts: 82
Joined: Sat Mar 15, 2008 8:13 pm

Re: including text from a "source" file

Post by dmt »

Thanks, but do you think you could explain to me what this does and how it works?
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Re: including text from a "source" file

Post by phi »

In the block environment, I compare the name of the block to be included to the actual block name. If the block should not be included, I put it into a box which is then just thrown away. Otherwise, the environment contents are left untouched and therefore wind up in the main file.
dmt
Posts: 82
Joined: Sat Mar 15, 2008 8:13 pm

Re: including text from a "source" file

Post by dmt »

Okay, but how would the body of the document look like with one or two examples, and what do I need to do in the master file to make it look right (a simple example would be appreciated).
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

including text from a "source" file

Post by phi »

exactly the way you suggested in your first post:

Code: Select all

%% masterfile.tex:
\begin{block}{abc}
ABC
\end{block}

\begin{block}{def}
DEF
\end{block}

\begin{block}{ghi}
GHI
\end{block}

%% some other file:
\documentclass{article}

\newsavebox\scratchbox
\newcommand*\includeblocklabel{}
\newcommand*\blocklabel{}
\newif\ifincludeblock

\newcommand*\includeblock[1]{%
  \renewcommand*\includeblocklabel{#1}%
  \input{masterfile.tex}%
}

\newenvironment{block}[1]{%
  \renewcommand*\blocklabel{#1}%
  \ifx\blocklabel\includeblocklabel
    \includeblocktrue
  \else
    \includeblockfalse
    \begin{lrbox}{\scratchbox}%
  \fi
}{%
  \ifincludeblock\else
    \end{lrbox}%
  \fi
}


\begin{document}

\includeblock{ghi}
\includeblock{abc}

\end{document}
dmt
Posts: 82
Joined: Sat Mar 15, 2008 8:13 pm

Re: including text from a "source" file

Post by dmt »

Great, thanks!
Post Reply