The m4 Macro Package

Author will write an article discussing m4, and illustrating it's use in two domains: building dynamic web pages, and using it to build a simple “database”.
A Simple Database Example

In this second example, we create a “database” of exam questions for a course. The goals are 1) to manage exam questions in a single repository, 2) to be able to create a LaTeX-format exam by simply choosing which questions to include and 3) to produce an answer key with equally little effort.

The question database consists of an m4 macro for each question. We store the macros in a file called questions.m4. For example:

define(`QUESTION_1',
  `Why did the chicken cross the road?
  ANSWER(1in, `To get to the other side')
')

Obviously, this macro will be expanded with the question itself, but note the use of the embedded macro ANSWER. It expands to one inch of vertical space if we're producing the exam, otherwise it expands to the answer if we're producing an answer key. ANSWER is defined as:

define(ANSWER, ifdef(`ANSWER_KEY', `Answer: $2',
`\vspace*{$1}'))
If ANSWER_KEY is defined, we include the answer ($2), otherwise we include some vertical space ($1) so the student can write in the answer.

Using the question macros is as easy as using the HTML macros. The complete exam code is stored in a file called exam.m4:

include(examMacros.m4)
EXAM_HEADER(`JOKE 101', `Fall 2001',
`First Exam')
include(questions.m4)
QUESTION_1
QUESTION_2
EXAM_TRAILER

The “include” on line one includes the code for EXAM_HEADER and EXAM_TRAILER that generate boilerplate LaTeX at the top and bottom of the exam. Line three includes the question macros we just created. Lines four and five include two questions from the database.

To create an exam, we run the command m4 exam.m4 > exam.tex. Because ANSWER_KEY was never defined, each question will include space for an answer. To create an answer key, we use m4's command-line options to temporarily define an ANSWER_KEY macro:

m4 -DANSWER_KEY exam.m4 > exam.key.tex
Conclusion

m4 is a tool that has applications in an endless number of domains. Anywhere you want to reduce duplication of effort, m4 can help. It is feature-rich enough that you can do almost anything and produce any kind of content you may wish. I hope I've given you enough to whet your appetite for m4 and to give you an appreciation for what m4 can do for you. Happy macro writing!

Resources

email: adams@csis.gvsu.edu

Robert Adams is a professor of computer science at GVSU. When he's not teaching he enjoys playing the fiddle, dancing and spending time with his daughter Turah.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Re: Take Command: The m4 Macro Package

Anonymous's picture

Extolling the virtues of m4. In 2002.
Wow.

Has anyone noticed that the whole using two different quote
markers is unpleasant, out of use, and hard to read? Not to
mention that nearly all scripting languages like python or perl
provide convenient means of text replacement already and
are generally better known, more well behaved quoting-wise
and end up being easier to read?

Let m4 die.

you can use the following to

Anonymous's picture

you can use the following to redfine quotes:

angequote([,])

Correction

Doug's picture

There's some typo here. The m4 builtin function for this is actually "Changequote()".

Exhaustive documentation is available for m4 -- try running "info m4" on your system. If that doesn't work, then try visiting

http://www.gnu.org/software/m4/manual/html_node/index.html.

m4 is amazingly configurable. If you don't like the way it works, chances are good that you re-configure to work in a way that you do like.

Re: Take Command: The m4 Macro Package

Anonymous's picture

I disagree to "let m4 die". Look, i can define a macro within a multiline comment of a java file. Since it is comment, java compiler does not protest. This macro is used to generate an aspectj file, compiled together with the ajc compiler. Thus, using m4 i can have a variety of application versions, by only pointing the compilers to the list of automatically generated files. And for production code i can go without aspectj. Try to do it with perl :)

sincerely,
Jacek Kempski

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix