5

I am a novice in LaTeX and I have just learnt how to make a macro and I used it to solve a problem. Please let me describe the problem and my solution.

Here is my problem.

\documentclass{article}
\begin{document}

% This is just one example of multiple such sections that would be
% present in this document.

% ----- Begin Example -----
% Company Name
\section*{FooCorp}
% Founded Date
\textbf{Founded 2013} \\
% Domain
\textit{Cross Platform Software} \\
% Location
\textit{Singapore}

% Short Introduction
FooCorp creates great products in Foo area.
FooCorp believes in uniformity and ease of use.

% Detailed Points
\begin{itemize}
\item FooCorp supports both Unix and Windows users. It conforms to
      existing Unix standards and bundles software with typical Windows
      installers.
\item FooCorp provides you end-to-end support regarding any problems
      encountered from the inception stage to go-live stage.
\end{itemize}

% Customers
Customers: Existing list of customers of FooCorp includes BarCorp,
BazCorp, QuxCorp.
% ----- End Example -----

% Another section similar to the above one would begin here. There would
% be multiple such sections in this document.
\end{document}

The above document is supposed to contain multiple sections where each section contains a company profile. I have shown only one such section here for the sake of brevity.

Now I want to decouple the data from presentation, e.g. if in future I decide that the 'Founded' date should be italicized or that the 'Customer' list should come before the 'Detailed Points', I shouldn't have to go and edit each section. I should be able to edit these stylistic decisions at a single place and all sections should be rendered according to it.

So I solved it with a macro like this.

\documentclass{article}

\newcommand{\corp}[6]{
\section*{#1}
\textbf{Founded #2} \\
\textit{#3} \\
\textit{#4}

#5

Customers: Existing list of customers of #1 includes #6.
}

\begin{document}

% This is just one example of multiple such sections that would be
% present in this document.

% ----- Begin Example -----
\corp{FooCorp}
     {2013}
     {Cross Platform Software}
     {Singapore}
     {
        % Short Introduction
        FooCorp creates great products in Foo area.
        FooCorp believes in uniformity and ease of use.

        % Detailed Points
        \begin{itemize}
        \item FooCorp supports both Unix and Windows users. It conforms to
              existing Unix standards and bundles software with typical Windows
              installers.
        \item FooCorp provides you end-to-end support regarding any problems
              encountered from the inception stage to go-live stage.
        \end{itemize}
    }
    {
        % Customers
        BarCorp, BazCorp, QuxCorp
    }
% ----- End Example -----

% Another section similar to the above one would begin here. There would
% be multiple such sections in this document.
\end{document}

But being a novice, it feels very unLaTeXy (if there's such a thing) to me. What seems odd is that some of the arguments to the macro are multiple-line arguments.

My questions:

  1. Is this really the right problem to solve using macros?
  2. Are there any drawbacks of this approach that I need to be aware of?
  3. How would you solve such a problem?
  • Well, one could frown on the usage of \\` in there (just leave an empty line), and it's possible to frown on the bunch of arguments there, but basically it's the way to go. You have to remember which argument is meant for which purpose of course. Perhaps a key-value syntax is easier? – user31729 Aug 28 '16 at 11:03
  • Is there any feedback on this? – user31729 Aug 30 '16 at 21:16
6

Some suggestions of improvements with using empty lines instead of \\ and applying a key-value interface. The advantage of using keys is that it is not important in which order they are specified!

(The usual complains about xkeyval will show up, I know ;-))

One must be aware that unset keys are either undefined or older values are taken again, this can be prevented by grouping!

\documentclass{article}

\usepackage{xkeyval}

\makeatletter

\define@key{corp}{sectionname}{\def\KVcorpsection{#1}}
\define@key{corp}{founded}{\def\KVcorpfounded{#1}}
\define@key{corp}{info}{\def\KVcorpinfo{#1}}
\define@key{corp}{otherinfo}{\def\KVcorpotherinfo{#1}}
\define@key{corp}{customers}{\def\KVcorpcustomers{#1}}
\makeatother

\newcommand{\corp}[2][]{%
\begingroup
\setkeys{corp}{#1}
\section*{\KVcorpsection}

\textbf{Founded \KVcorpfounded} 

\textit{\KVcorpinfo} 

\textit{\KVcorpotherinfo}

#2%

Customers: Existing list of customers of \KVcorpsection includes \KVcorpcustomers.
\par
\endgroup
}

\begin{document}

% This is just one example of multiple such sections that would be
% present in this document.

% ----- Begin Example -----
\corp[sectionname=FooCorp, customers={BarCorp, BazCorp, QuxCorp},info={Cross Platform Software}, otherinfo={Singapore}, founded={2013}]
     {% Short Introduction
       FooCorp creates great products in Foo area.
       FooCorp believes in uniformity and ease of use.

       % Detailed Points
       \begin{itemize}
       \item FooCorp supports both Unix and Windows users. It conforms to
         existing Unix standards and bundles software with typical Windows
         installers.
       \item FooCorp provides you end-to-end support regarding any problems
         encountered from the inception stage to go-live stage.
       \end{itemize}
     }
% ----- End Example -----

% Another section similar to the above one would begin here. There would
% be multiple such sections in this document.
\end{document}
  • 1
    it would be good to add an explicit \par before the \endgroup, just in case the font size changes, so that proper baselines are preserved. – barbara beeton Aug 28 '16 at 12:43
  • @barbarabeeton: Done, thanks for suggestion! – user31729 Aug 28 '16 at 13:53
  • @ChristianHupfer when I ran your code, I got Undefined control sequence. \define@key. What did I miss? – Diaa Aug 29 '16 at 19:27
  • @DiaaAbidou: I don't know what you're missing :D The code runs with TL 2016 (and should even run with older versions, there's nothing sophisticated in it) Did you use xkeyval package and take care of the \makeatletter...\makeatother pair? – user31729 Aug 29 '16 at 20:37
  • 1
    @DiaaAbidou: KV stands for key value -- it's a usual naming scheme for macros that hold the key-values, others use \KV@foo or \kv@foo etc, but with the @- letter I have to wrap a \makeatletter...\makeatother pair around the macro using the keys as well in a document (not in the class or package, of course) – user31729 Aug 30 '16 at 4:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.