Text FormattingMacros inside Macros inside Macros...

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
kilojulius
Posts: 2
Joined: Tue Jan 20, 2015 2:48 pm

Macros inside Macros inside Macros...

Post by kilojulius »

Hi,

I'm writing the whole document in macros and didn't find a problem in the MWE and also publishing a 50 page manual this way. Just for learning I'ld like to know whether this is good or bad practice. Maybe it's very risky? In the real document I use other macros for my colleagues to easily add Symbols or Formatting for Bits of Text. This can lead up to a level of four - Macro inside Macro. Are there environments or commands that are not working inside Macros?

Question 1: Is it risky to write all body text in this type of Macro?
Question 2: Are there environments or commands that are not working inside Macros?

Code: Select all

\documentclass{article}
\usepackage{xcolor}

\newcommand{\Enterareaone}[1]	
{									
\vskip 7pt
{{\bf {\color{red} This is Areaone}} \\}
{#1\\}
}

\begin{document}


\Enterareaone
{
This is the text that is written below the title of the area which is ''This is Areaone''
}

\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Macros inside Macros inside Macros...

Post by Johannes_B »

Hi and welcome,

expansion is a tricky thing in TeX, very tricky. To be honest, i don't have the knowledge of the lower level stuff. But on the level you are currently working, you should be fine.

But please remember that there are environments as well. Don't put everything in macros, enclose it in an environment. Some macros cannot process arguments which are long, i.e. containing a paragraph (an empty line).

If using LaTeX3 syntax is offputting right now for you, which is perfectly understandable, at least use LaTeX2e syntax. You are using a mix of TeX and LaTeX2.09 (which went obsolete 20 years ago).

The more time and effort you spend on designing a solid userinterface, the less probable is the chance of breaking later on. I suppose the text could be a bit longer, so i used an environment here. Advantage, you could later redefine the environment to use a frame more easy.

Code: Select all

\begin{filecontents}{\jobname.sty}
\newlength{\areaone@Pretitle}
\setlength{\areaone@Pretitle}{1ex}
\newcommand{\areaonePostTitle}{}
\newkomafont{areaonefont}{\normalfont\bfseries\color{red}}
\newenvironment{areaone}{\par%Just to be sure
	\vspace{\areaone@Pretitle}%
\noindent{\usekomafont{areaonefont}This is Areaone\par}%
\areaonePostTitle
}{\par}
\end{filecontents}

\documentclass{article}
\usepackage{scrextend}
\usepackage{xcolor}
\usepackage{csquotes}
\usepackage{\jobname}
\usepackage{mdframed}

\newcommand{\Enterareaone}[1]  
{                                                                      
\vskip 7pt
{{\bf {\color{red} This is Areaone}} \\}
{#1\\}
}

\begin{document}


\Enterareaone
{
This is the text that is written below the title of the area
which is ''This is Areaone''
}

\rule{.5\textwidth}{.4pt}
\begin{areaone}
This is the text that is written below the title of the area
which is \enquote{This is Areaone}
\end{areaone}
\addtokomafont{areaonefont}{\itshape}
\surroundwithmdframed{areaone}
\begin{areaone}
This is the text that is written below the title of the area
which is \enquote{This is Areaone}
\end{areaone}



\makeatletter
\setlength{\areaone@Pretitle}{3ex}
\makeatother
\renewcommand{\areaonePostTitle}{\noindent\rule{\textwidth}{.4pt}\par}
\begin{areaone}
This is the text that is written below the title of the area
which is \enquote{This is Areaone}
\end{areaone}
\end{document}

Please take special care of non protected line endings, they all produce spaces, mostly at places where you don't want them.
\\ are linebreaks and have nothing to do with paragraphs. A double backslash in normal text should always be a warning sign and lead to suspicion. Use a blank line, or \par in your definitions. What is the difference? Since LaTeX (TeX) works with paragraphs, it gets confused seeing only one reaaaaally big paragraph.


Macros can be used solely to save some time typing, if you notice that is the reason for you doing it, stop right there. This will go bad. But defining many little helper macros to ensure a consistent outcome for everybody contributing, and/or providing hooks (in form of helper macros) to easily adjust the output. Like above, there is an easy interface to change the font of the title (or whatever the red thing is). You could also, instead of setting a fixed length, use LaTeX lengths.


The standard classes, which are based on code by Leslie from the eighties defined stuff the fixed way (just like you did). This is one reason why so many packages were developed that provided hooks to easily adjust to ones personal taste.

On the other hand, there are classes like memoir or the KOMA-classes that are going the approach i am using above. Look ahead, where could the user want to make changes? Where am i allowing the user to do changes?

I once again changed the above code, hiding the definition of the environment in a small little package. Back to allowing the user stuff: To change the length \areaone@Pretitle the user has to do some extra stuff that warns him: be careful, you are messing around with internal stuff.


Where can stuff break? Think about captions, their argument is usually processed right with the object and once again in the list of objects. Fragile commands are breaking (hence the name) so you have to take care there.


If you have further questions, feel free to ask. :-)
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
kilojulius
Posts: 2
Joined: Tue Jan 20, 2015 2:48 pm

Re: Macros inside Macros inside Macros...

Post by kilojulius »

Hi,

thanks for your coaching. I'll go through the options and tell you what worked best. The goal is an interface with maximum convenience which will also come with global settings controling the outcome. For example, there's two global switches (\ifdefined) in the project that allow different macros being switched on and off.

I'll try and adapt in terminology. I think the "expansion" you were talking about is what my macro does? Like expanding text inside a macro with the settings and additional text (the title) that is managed globally?

So long...
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Macros inside Macros inside Macros...

Post by Johannes_B »

Expansion is a serious topic in the world of TeX and LaTeX. I suggest to read the TeXbook, the original manual written by Don Knuth for TeX.

Seems like you want to write your own class. Have a look at other classes, especially those of journals, how they use and handle options. (Disclaimer: the code is often outdated or just bad)

A further reading could be texdoc clsguide, a guide for package and class authors along with the documentation to package etoolbox.

The most flexible outcome can be produced using the new features of LaTeX3. If you have a bit of coding practice, this should be fairly easy.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
shaista
Posts: 1
Joined: Sun Mar 15, 2015 2:15 pm

Re: Macros inside Macros inside Macros...

Post by shaista »

Any help would really be great.
I have been given a 2 columns paper.

The first line of the latex file is:
\documentclass[twocolumn]{article}

Everything is fine as far as 2 columns are concerned but now I have to insert a big table which is not accommodating, so I have to change to 1 column just for one page, then again for next page it will again two page article. how can i achieve this target
User avatar
Stefan Kottwitz
Site Admin
Posts: 10348
Joined: Mon Mar 10, 2008 9:44 pm

Re: Macros inside Macros inside Macros...

Post by Stefan Kottwitz »

Welcome to the forum!

How is this problem related to this topic "Macros inside Macros inside Macros..."? Are you nesting macros? Anything related to the posts above?

If you got a new, independent question, feel free to open a new topic. People better see it, compared to a post attached below an old post with different subject.

Stefan
LaTeX.org admin
Post Reply