The m4 Macro Package
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
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.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- RSS Feeds
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Designing Electronics with Linux
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Validate an E-Mail Address with PHP, the Right Way
- What's the tweeting protocol?
- Kernel Problem
6 hours 21 min ago - BASH script to log IPs on public web server
10 hours 48 min ago - DynDNS
14 hours 24 min ago - Reply to comment | Linux Journal
14 hours 56 min ago - All the articles you talked
17 hours 20 min ago - All the articles you talked
17 hours 23 min ago - All the articles you talked
17 hours 24 min ago - myip
21 hours 49 min ago - Keeping track of IP address
23 hours 40 min ago - Roll your own dynamic dns
1 day 4 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
Re: Take Command: The m4 Macro Package
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
you can use the following to redfine quotes:
angequote([,])
Correction
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
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