18

This has sort of been touched on in some other postings, but for path reasons I would like to avoid using a shell script and just use ImageMagick's (IM) convert command line executable directly from a \immediate\write18{...} in a macro command, but in my IM convert instructions I need to pass the % (ASCII 37) as part of a macro.

However I try to conceive of making a % - and I have tried numerous ways(!) I always end up getting the Latex code I am using for the % passed through to the command line.

The obvious one, \% goes to the shell as \% directly and that is the pattern followed by all other options.

What I obviously seem to need is some way to generate the ASCII or unicode for % and get that to the shell from LaTeX via \write18 or other.

Or have some sort of temporary suspension on the LaTeX usage of % so that it can be part of a \write18{} and not break the command as a comment starter, and so be passed with out needing to be LaTeX escaped first.

2
  • 2
    Did you try \makeatletter \immediate\write18{...\@percentchar ..}? May 5, 2011 at 11:50
  • @UlrikeFischer: Is there a macro for #? I have tried \@sharpchar but it does not exists. Nov 5, 2013 at 17:01

2 Answers 2

14

Use \@percentchar instead of %. You need to say \makeatletter before the \write and \makeatother after it to allow for the @ in the macro name if the code isn't located in a package or class or already inside a \makeatletter ... \makeatother block.

There are also \@backslashchar for \ and also \@charlb and \@charrb for { and }. The are listed as "String constants" in my macros2e document as mentioned in Documentation reference for LaTeX internal commands?

You can also now easily define new string macros for any set of characters with the new version of the newverbs package. E.g. to define a macro for the ampersand use:

\usepackage{newverbs}[2011/07/24]
\Verbdef\amp{&}

Note that spaces behind macros are removed also in a \write statement, so you might need to manually insert one using \space.

3
  • Thanks very much Martin, From comp.text.tex there are some very useful concepts I just found before I got notification of your post. groups.google.com/group/comp.text.tex/browse_thread/thread/… Things like creating temporary batch files form within LaTeX directly where necessary 'difficult' characters can be written directly, and use of \string& and \@percentchar (with (See arf1.de/wordpress/?p=436). May 7, 2011 at 4:15
  • @Martin Scharrer Hi I've had login in problems and only just got back, but I can not see a tick symbol on this page at all Martin - nearly sent post then found it not on the right bit on the left-went crazy trying to work out where it was on the upper right! thanks, Paul Nov 6, 2011 at 5:23
  • @PaulANormanNZ: Sorry, I meant upper left. ;-) Nov 6, 2011 at 7:49
2

Here's my two cents about writing percent characters to file:

enter image description here

\documentclass{article}
\usepackage{verbatim}
%
\newwrite\tmpfile
%
\begingroup\lccode`\S=`\%\relax\lowercase{\endgroup\def\mypercent{S}}%
\newcommand\mysecondoftwo[2]{#2}%
%
\immediate\openout\tmpfile test.txt
\immediate\write\tmpfile{Approach 1: Some text and a percent sign\mypercent}%
\immediate\write\tmpfile{Approach 2: Some text and a percent sign\expandafter\mysecondoftwo\string\%}%
\immediate\write\tmpfile{Approach 3: Some text and a percent sign\csname @percentchar\endcsname}%    
\immediate\write\tmpfile{Approach 4: Some text and a percent sign\csname @secondoftwo\expandafter\endcsname\string\%}%        
\makeatletter
\immediate\write\tmpfile{Approach 5: Some text and a percent sign\@percentchar}%
\immediate\write\tmpfile{Approach 6: Some text and a percent sign\expandafter\@secondoftwo\string\%}%
\makeatother
\begingroup
\def\textmacro{Approach 7: Some text and a percent sign}%
\lccode`\S=`\%%
\lowercase{\immediate\write\tmpfile{\textmacro S}}%
\endgroup
\immediate\closeout\tmpfile

\begin{document}
\verbatiminput{test.txt}
\end{document}
2
  • If I need to run the command wolframscript & echo %ERRORLEVEL% > latexalpha2_check.tmp on Windows PC, what changes should be done to \immediate\write18{wolframscript & echo %ERRORLEVEL% > latexalpha2_check.tmp}?
    – Diaa
    Jan 18, 2021 at 0:38
  • @Diaa In LaTeX 2e: \immediate\write18{wolframscript \string& echo \csname@percentchar\endcsname ERRORLEVEL\csname@percentchar\endcsname\space> latexalpha2\string_check.tmp}. Perhaps better: \NewDocumentCommand\ReadVerbatimAndImmediateWriteEighteen{v}{\immediate\write18{\detokenize{#1}}}...\ReadVerbatimAndImmediateWriteEighteen|wolframscript & echo %ERRORLEVEL% > latexalpha2_check.tmp| Jan 18, 2021 at 11: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.