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 »

Hmm, it's not working. I will include the two simple files I am using to test. One is called problems.tex, the "master file"
problem.tex

Code: Select all

\begin{block}{vg1}
\question testing one
\end{block}


\begin{block}{vg2}
\question testing two
\end{block}
test.tex

Code: Select all

\documentclass[a4paper,11pt]{exam}
\usepackage[swedish]{babel}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{graphicx}
\usepackage{float}
\usepackage{wasysym}


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

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

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



\begin{document}



\begin{questions}

%\includeblock{vg1}

\end{questions}
\end{document}

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

including text from a "source" file

Post by phi »

OK, the \question command is the problem here. It expects to be in a list environment, but is not if the block is gobbled. You have to disable \question temporarily by inserting

Code: Select all

\let\question\relax
after \includeblockfalse.
dmt
Posts: 82
Joined: Sat Mar 15, 2008 8:13 pm

Re: including text from a "source" file

Post by dmt »

Okay, it seems to be working now. Thanks.
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

including text from a "source" file

Post by phi »

If you do not mind giving up the environment syntax, then a simpler and more robust solution is possible:

Code: Select all

\newcommand*\ifblock[1]{%
  \renewcommand*\blocklabel{#1}%
  \ifx\blocklabel\includeblocklabel
}
Then the block have to look like this:

Code: Select all

\ifblock{abc}
ABC
\fi
Post Reply