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:

  `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',
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:

EXAM_HEADER(`JOKE 101', `Fall 2001',
`First Exam')

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

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!



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.



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.

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:



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

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 :)

Jacek Kempski

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState