OthersInvoke M4 macrocompiler from LaTeX (AUCTeX)

Information and discussion about other tools not listed above.
Post Reply
beaver
Posts: 6
Joined: Wed Feb 23, 2011 12:04 pm

Invoke M4 macrocompiler from LaTeX (AUCTeX)

Post by beaver »

Hi all,

Once again i have a nasty problem i could not get rid of so far. Since i know no better place to post this (i cannot really localise the problem), i will try in this section :-)

So what i would like to do is drawing electronic circuits and other stuff in an convenient way in combination with Latex. Searching the internet, i found the M4 - macro compiler [1] and appropriate macros for electric circuits [2].
Together with an pic-interpreter [3] the macro files (.m4) first get converted to a .pic file which again the interpreter dpic then converts to a .tex file that may be included into a latex document. This is also illustraed on page in two [2].

Now to the problem:
I installed the dpic interpreter as well as the M4 "compiler" as well as the M4 circuit macros as described in the according README files.
The test of the installation was successful, it works to start with an .m4 file, process this file with m4 and afterwards with dpic to get a latex file.

The process of conversion which results in an .dvi file happens by typing the following commands to the cmd shell of windows:
(where ex07.m4 is the source file to be processed taken from the examples directory of the m4 circuit macro zip file)

Code: Select all

m4 C:\_LocalLatex\tex\latex\Circuit_macros\pstricks.m4 C:\_LocalLatex\tex\latex\Circuit_macros\libcct.m4 ex07.m4 > ex07.pic
dpic -p ex07.pic > ex07.tex
latex Dok.tex
Now, everything works fine to this point.
What i want to do next is to get rid of typing in these few lines everytime i want to "compile" a new drawing, so it would be nice to get this conversion process into the Latex source (as i already mentioned - i use auctex, just for the case that this might be a problem ;-) ).

How one can do the conversion on tex - source level is also described in [2] as the last point of possible simplifications on page 4.

The writer there says, that this would work by defining a new latex command as follows:

Code: Select all

\newcommand\mtopgf[1]{\immediate\write18{m4 <path>/pgf.m4 #1.m4 | dpic -g > #1.tex}}%
According to what i have written above, of course, the <path> in my case is:
C:\_LocalLatex\tex\latex\Circuit_macros\

I have set all the Path variables as described and also tried to call latex with the --shell-escape postfix, which is obviously needed to allow the \write18 operation.

But when i start the Latex compilation process, it gives me the following error(s)

Code: Select all

! Undefined control sequence.
<write> m4 C:\_LocalLatex\tex 
                              \latex \Circuit _macros\pgf .m4 ex07.m4 | dpic...
l.3 \mtopgf{ex07}
                 
! Undefined control sequence.
<write> m4 C:\_LocalLatex\tex \latex 
                                     \Circuit _macros\pgf .m4 ex07.m4 | dpic...
l.3 \mtopgf{ex07}
                 
! Undefined control sequence.
<write> m4 C:\_LocalLatex\tex \latex \Circuit 
                                              _macros\pgf .m4 ex07.m4 | dpic...
l.3 \mtopgf{ex07}
                 
! Undefined control sequence.
<write> ...lLatex\tex \latex \Circuit _macros\pgf 
                                                  .m4 ex07.m4 | dpic -g > ex...
l.3 \mtopgf{ex07}
I should mention here, that putting the (exactly same) command as it gets defined by the \newcommand line manually into the cmd.exe, the .dvi file is compiled without any problems. (Because this is exactly what was done to check the functionality of the installation.)
So it must have to to with a problem Latex has with this defined \newcommand.

I know, this is a quite long post, hopefully i have not forgotten something importnat , otherwise please let me know.
It would be wonderful if someone had a good idea, what i could try next ;-)

Kind regards,
Franz


[1] : http://www.gnu.org/software/m4/
[2] : ftp://www.dante.de/tex-archive/graphics ... /CMman.pdf
[3] : https://ece.uwaterloo.ca/~aplevich/dpic/

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
Laganne
Posts: 5
Joined: Thu Mar 10, 2011 1:33 pm

Re: Invoke M4 macrocompiler from LaTeX (AUCTeX)

Post by Laganne »

The backslashes in the path to pgf.m4 are the problem, since LaTeX is
trying to expand parts of the path as macros. One workaround is to
omit the first file read by m4 in the definition of mtopgf so that it reads
m4 #1.m4 ... but then the first line of each diagram source must be
include(<path>pgf.m4)
followed by other include lines as necessary.
beaver
Posts: 6
Joined: Wed Feb 23, 2011 12:04 pm

Invoke M4 macrocompiler from LaTeX (AUCTeX)

Post by beaver »

Hmm, i see you are obviously right, thanks a lot! :-)
Avoiding the path in the command and therefore the "\" characters allows the execution of the command within Latex without any errors.

One nasty thing that remains using the command in the form

Code: Select all

\newcommand\mtopgf[1]{\immediate\write18{m4 #1.m4 | dpic -g > #1.tex}}%
is, that i don´t know how to change the path of the output file created by M4.
This is necessary, since in the current form i cannot include the (by dpic) created .tex file via the \input command into another .tex document of my documentation because dpic drops the .tex file at the location of the given .pic input file.
M4 should drop the created .pic file always at he path from which M4 was invoked.
When the (manual) input to the cmd.exe looks like
  • C:\testdir>m4 D:\DATA\docu\ex07.m4 > test.pic

then M4 drops the file test.pic in the directory C:\testdir as i expected.

But when i used the \mtopgf command in latex as written above, M4 does not drop the .pic file at the path from which i "called" the \mtopgf command. (The folder of the corresponding chapter of the latex documentation files).

Instead, the file always appears in C:\Users\Username and dpic drops the resulting .tex file also there, so the \input command of latex would not find it :-)
Of course, i could point the \include command to this location, but that´s not really what i want ;-)

Thanks and kind regards,
Franz
Laganne
Posts: 5
Joined: Thu Mar 10, 2011 1:33 pm

Re: Invoke M4 macrocompiler from LaTeX (AUCTeX)

Post by Laganne »

The \write18 statement writes to the operating system. The
commands executed by the operating system depend on what
the system is and, perhaps, on environment variables. You
might try the following:
Write a script that goes a suitable working directory,
invokes m4 on the source file,feeds the result to
dpic, and copies the .tex result where you want it. What
the script will look like depends on your operating system and
the scripting language. Then the \write18 command just has
to call the script with one argument, the base name of the
.m4 source file.
beaver
Posts: 6
Joined: Wed Feb 23, 2011 12:04 pm

Re: Invoke M4 macrocompiler from LaTeX (AUCTeX)

Post by beaver »

Hmm... ok, i will try that within the next days.
However, since i am not really a programmer and haven´t done any scripting so far, this will take me some time.
(But i think it will not be that hard to immediately try out your suggestions within a simple .bat file in windows :-) )

Thanks,
Franz
beaver
Posts: 6
Joined: Wed Feb 23, 2011 12:04 pm

Invoke M4 macrocompiler from LaTeX (AUCTeX)

Post by beaver »

Ok, it´s been a while since my last post, i was quite busy ;-)

However, i now have managed it to let M4 put the files into the folder where i need it.
Due to the lack of time, i have not written a script since for my purpose i found it sufficient that the created .tex file from M4 and Dpic is put within the same location where the .m4 file resides.

I have accomplished this by simply using the command
  • \newcommand{\mtopgf}[1]{\immediate\write18{m4 \pwd/#1.m4| dpic -p > \pwd/#1.tex}}%
in combination with renewing the \pwd comamnd each time i include a new chapter (which corresponds to a new folder in the documentation hirachy).
Therefore, including happens like this.
  • \newchapter{Introduction}{Introduction}
    \renewcommand{\pwd}{chapter0}
    \input{\pwd/introduction}
As already mentioned above, this is a kind of workaround, but it´s totally ok for me, so i won´t put more effort in this topic for now(this is why i would like to mark this topic as solved, but i cannot edit my very first post :-)).

Thanks for guiding me into the right direction!

Kind regards,
Franz
Post Reply