Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a rather large block of TeX macro code that is no problem to do in TeX but I'd like to create the macro in Lua instead

The macro contains various TeX macro characters (\, %, [, ])

How can I easily format this macro easily without having to really mess with it much by hand so I can use tex.print to get it into TeX?

I've tried using [[ ]] and \n after the %'s and various other things but nothing makes TeX happy or it can't find the macro if it does work.

share|improve this question
1  
In tex.stackexchange.com/questions/48414/… you also talk about including TeX material inside Lua code. Could you clarify the difference between the two situations? –  Joseph Wright Mar 29 '12 at 6:08
    
one is inside lua and one is inside lualatex. lua = inside a lua file the other inside a tex file. e.g., one uses directlua(or whatever lualatex block) and the other is inside a lua file. –  Uiy Apr 3 '12 at 12:54

2 Answers 2

up vote 9 down vote accepted

You probably talking about heredoc syntax in Lua, guessing from your comment

I've tried using [[ ]]

Let us assume you are writing a macro that you need to capture a rather long piece of text. You can enclose it as:

    [[..]]

If it encloses other square brackets, you need to escape it as:

    ([=[[[Anger Management]]
    ...        
    ]=])

Here is a full minimal:

\documentclass{article}
\usepackage[listings]{tcolorbox}
\begin{document}
\begin{tcblisting}{}
\ttfamily \directlua{
 tex.sprint([=[
    [[Anger Management]]Mild-mannered timid businessman Dave Buznik who works for a pet clothing company out of New York City. He's got an abrasive boss named Mr. Frank Head who frequently takes credit for his work and steps on him in return. He's got a loving girlfriend, Linda, whose best friend is her condescending college ex, Andrew. But when a misunderstanding aboard an airplane goes haywire, Dave is ordered by the court to undergo anger management therapy at the hands of specialist Dr. Buddy Rydell, who is an unpredictable, psychopathic character. As the relationship between Dave and Buddy becomes more tense, when the unorthodox treatment wreaks havoc Dave's life, and Buddy might be the only one who can save him from a problem he recognizes right away in his patient, that could only get worse.
]=])
}
\end{tcblisting}
\end{document}

This should produce the following output:

enter image description here

As discussed earlier programming needs patience.

share|improve this answer
    
This doesn't work. The macro I'm trying to tex.print is tex.stackexchange.com/questions/49800/tikz-fading-speed. I've tried removing the makeat stuff too and other various things but nothing seems to work. –  Uiy Mar 29 '12 at 3:02
2  
@Uiy Ah, are you trying to tex.print stuff which would have a different category code to the 'standard' ones? As discussed in tex.stackexchange.com/questions/45293/…, you will need to set this up explicit in Lua, as TeX catcode changes will not apply. –  Joseph Wright Mar 29 '12 at 6:11
    
well, this is probably the issue. I'm using tex.print inside a lua file and figured it would just get sent to the tokenizer but I guess special tex symbols are being treated as normal letters and not being parsed right. What one needs is the ability to take a string literal and parse it and add the proper catacodes to the tex symbols to make it work. (I'm able to do basic macro's without issue which is probably why I'm confused. I'm not sure what is breaking) –  Uiy Apr 3 '12 at 12:56
    
I don't know a hole lot about the catacodes stuff except they are extra meta information about what tokens are and the tex interpreter will react differently based on the codes. I'm think if I can easily change the catacode for the problematic symbols I could simply write a parser in lua to do the job of adding the catacodes. Any ideas if this is already done or what symbols I would need to change? –  Uiy Apr 3 '12 at 13:00

While it is unclear from your question what you want to do, I try to answer it.

  1. Never use \directlua, use the environment luacode* from the luacode package.
  2. When using the lua functions tex.sprint() or tex.print() use "-2" as the first parameter. This is a catcode table with safe catcodes. All characters such as %, $, \ have the meaning "letter" and thus don't do harm.
  3. If you do need a command name in between, use tex.tprint({},{},{},{}...). In these tables you can optionally give the catcode as the first element: tex.tprint({"\\command{"},{-2,"%$~safe"},{"}"}) which yields:

    \command{%$~safe}
    

with the "meaningless" characters %$~safe

share|improve this answer
    
As I said, I'm doing this from lua and hence not inside a tex file. Hence I wouldn't need to change the catcodes as you mention(but maybe I have to actually do the reverse?). –  Uiy Apr 3 '12 at 12:55
    
@Uiy No, my code is for "inside a Lua file" (or from a luacode(*) code block). If it does not work for you, I'd kindly ask to rephrase your question and provide a minimal example. Did you try my code? What went wrong? –  topskip Apr 3 '12 at 15:38
    
Oh, ok, I thought by luablock you meant that it is inside a tex file. I think a luablock is not quite exact as a lua file? (very close but slightly different IIRC) I haven't tried the code yet as I worked around it before but I will try soon with something new after I solve another problem. –  Uiy Apr 3 '12 at 15:49
    
@Uiy The environment luacode* from the luacode package is very close to writing the Lua code in a separate file. But you get better error handling if you put the code into a Lua file. –  topskip Apr 3 '12 at 15:51
    
yep, that's what I've learned and I've moved all my commmon code to lua files. –  Uiy Apr 3 '12 at 15:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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