General\write curly brace

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
stringkarma
Posts: 7
Joined: Tue Jun 23, 2009 10:59 pm

\write curly brace

Post by stringkarma »

I know this is a very specific issue but it is important to me, I hope someone knows how to solve it!

I am trying to write a line to a text file (not unlike creating the aux file). I want to write out the line:

PageProps = {

to a file.

I can write:

PageProps =

or:

PageProps = {}

but if I try what I want, I get an error because of unbalanced braces.

Try it out with this

\newwrite\outputstream
\immediate\openout\outputstream=document.pdf.info
\immediate\write\outputstream{PageProps = { }

note that \{ doesn't seem to do it.

Recommended reading 2024:

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

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

\write curly brace

Post by josephwright »

Code: Select all

\immediate\write\outputstream{PageProps = \string{ \string} }
Joseph Wright
stringkarma
Posts: 7
Joined: Tue Jun 23, 2009 10:59 pm

Re: \write curly brace

Post by stringkarma »

Thank you for your quick reply, unfortunately this again only makes a pair of braces; I need to write out only a left brace { at the end.

Is there some way of doing this?
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

\write curly brace

Post by josephwright »

Use a group, and inside replace { } with, say, [ ], as group begin/end characters:

Code: Select all

\newwrite\outputstream
\immediate\openout\outputstream=document.pdf.info
\begingroup
  \catcode`\[ = 1\relax
  \catcode`\] = 2\relax
  \catcode`\{ = 12\relax
  \catcode`\} = 12 \relax
  \immediate\write\outputstream[PageProperies = { ]
\endgroup
This is all plain TeX coding, I'm afraid.
Joseph Wright
stringkarma
Posts: 7
Joined: Tue Jun 23, 2009 10:59 pm

\write curly brace

Post by stringkarma »

Excellent, just a little massaging and that is now working.

I have just one more question then. What if I wanted a command to write out a } to the file and close it. I am trying

Code: Select all

\providecommand{\finalizeinfo}[0]{
\begingroup
  \catcode`\[ = 1\relax
  \catcode`\] = 2\relax
  \catcode`\{ = 12\relax
  \catcode`\} = 12\relax
  \immediate\write\outputstream[ } ]
\endgroup
\immediate\closeout\outputstream
}
but it doesn't seem to accept this as a command that I can define. The method works for generating a } without trying to call it as \finalizeinfo.

Thanks again for all the help!
stringkarma
Posts: 7
Joined: Tue Jun 23, 2009 10:59 pm

Re: \write curly brace

Post by stringkarma »

One more note. The fix for the first problem works, but still gives an error message. Is there someway to turn that off? Just curious.
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

\write curly brace

Post by josephwright »

A better method:

Code: Select all

\newwrite\outputstream
\immediate\openout\outputstream=document.pdf.info
\begingroup
  \catcode`\[ = 1\relax
  \catcode`\] = 2\relax
  \catcode`\{ = 12\relax
  \catcode`\} = 12 \relax 
  \gdef\OpenBrace[{]
  \gdef\CloseBrace[}]
\endgroup
\immediate\write\outputstream{PageProperies = \OpenBrace }
\immediate\write\outputstream{\CloseBrace}
You can then use \OpenBrace and \CloseBrace wherever you need.
Joseph Wright
stringkarma
Posts: 7
Joined: Tue Jun 23, 2009 10:59 pm

Re: \write curly brace

Post by stringkarma »

Thanks so much, that works perfectly!
Post Reply