I mean C++ doing some calculation not inserting the code as a text.
[Tex/LaTex] Is it possible to insert C++ code into LaTeX
codeprogramming
Related Solutions
I'm not sure there is a good guide to refactoring TeX code, but a few things I note from the above you might want to think about:
Making global assignments should always make you think 'is this really needed?'. While making things global is quite often easy, other approaches are sometimes better, for example
\def\TempResult{Result}% \expandafter\endgroup \expandafter\def\expandafter\RealResult\expandafter{\TempResult}%
Watch how you deal with
\fi
. Remember that unless you expand the end of the conditional then they continue to stack up, and can lead to runaway conditions or exhaustion of the stack. This is why approaches such as\expandafter\NextFunction \fi
or
\if<something> \expandafter\@firstoftwo \else \expandafter\@secondoftwo \fi {True code} {False code}%
are very common (often the second is pre-packed, for example in
etoolbox
.Unless you want maximum performance, I'd favour using more, well-named macros over fewer longer ones, and favour macros over primitives (so
\@namedef
rather than\expandafter\csname ...\endcsname
, for example). (If you are really after maximum performance, then even additional\expandafter
s make a difference, but unless you are writing low-level kernel functions this should not be an issue!)
listings
packages will help you.
Using style=A
Using style=B
TeX Input File
\documentclass[dvipsnames,cmyk]{article}
\usepackage{listings,xcolor}
\lstset
{
breaklines=true,
tabsize=3,
showstringspaces=false
}
\lstdefinestyle{Common}
{
extendedchars=\true,
language={[Visual]Basic},
frame=single,
%===========================================================
framesep=3pt,%expand outward.
framerule=0.4pt,%expand outward.
xleftmargin=3.4pt,%make the frame fits in the text area.
xrightmargin=3.4pt,%make the frame fits in the text area.
%===========================================================
rulecolor=\color{Red}
}
\lstdefinestyle{A}
{
style=Common,
backgroundcolor=\color{Yellow!10},
basicstyle=\scriptsize\color{Black}\ttfamily,
keywordstyle=\color{Orange},
identifierstyle=\color{Cyan},
stringstyle=\color{Red},
commentstyle=\color{Green}
}
\lstdefinestyle{B}
{
style=Common,
backgroundcolor=\color{Black},
basicstyle=\scriptsize\color{White}\ttfamily,
keywordstyle=\color{Orange},
identifierstyle=\color{Cyan},
stringstyle=\color{Red},
commentstyle=\color{Green}
}
\begin{document}
\begin{lstlisting}[style=A]%please try style=B
Option Explicit
Sub Signal(strSignalfolge As String, Optional lngTakt As Long = 100)
'Prozedur erzeugt eine Serie von Warntönen.
'Die optionale Variable lngTakt gibt den Takt in Millisekunden vor (Standard: 100 ms)
'Das Muster kann über die String-Variable strSignalfolge beeinflusst werden:
' Stern (*) -> 1 Warnton
' Ziffern 1..9 -> 1..9 Takte Pause
' Leerzeichen -> 1 Sekunde Pause
' Minus (-) -> 1.5 Sekunden Pause
Dim i As Integer
Dim b As String
For i = 1 To Len(strSignalfolge)
b = Mid(strSignalfolge, i, 1)
Select Case b
Case "*": beep
Case 1 To 9: DELAY CInt(b) * lngTakt
Case " ": DELAY 1000
Case "-": DELAY 1500
End Select
DELAY lngTakt
Next i
End Sub
Public Function strParse(Data As String, Trenn As String, Nr As Integer)
'Funktion trennt die Zeichenkette <Data>
On Error Resume Next
Dim MainData() As String, SplitData() As String
MainData = Split(Data, Trenn)
SplitData = Split(MainData(Nr - 1), Trenn)
strParse = SplitData(0)
End Function
Public Sub ProtokollZeile(strData As String)
'Gibt eine Protokollzeile im Direktfenster aus: Datum, Uhrzeit und <strData>
Debug.Print Now & " " & strData
End Sub
Sub NetSend(strmsg As String, Optional strEmpf As String = "imf3beck")
'Prozedur sendet die Kurznachricht <strMsg> über das Intranet an den Rechner <strEmpf>
Dim a
a = Shell("cmd.exe /c net send " & strEmpf & " " & strmsg, vbMinimizedFocus)
MsgBox "(net send message)" & vbCr & strmsg
End Sub
Sub NetSendMessungBeendet(Optional strBem As String = "")
'Sendet über das Netzwerk eine Statusnachricht, dass die Messung beendet wurde und gibt diese
'Nachricht auch lokal als Warndialog aus
Dim strMsgText As String
strMsgText = (Format(Now, "hh:mm:ss") & " Messung beendet" & strBem)
NetSend (strMsgText)
End Sub
Public Function ZellBereichAdresse(strZellber As String) As String
Application.Volatile
ZellBereichAdresse = CStr(Range(strZellber).Address)
End Function
\end{lstlisting}
\end{document}
Notes:
- It is better to import the VBA source code from within your TeX input file using
\lstinputlisting[<options>]{filename.vb}
. This way will make your TeX input file clean. - Try my style B by changing
style=A
tostyle=B
.
Best Answer
In this solution I use the facilities of
fancyvrb
to write the C++ source code to a file calledhello.cpp
. Then I proceed to compile the written file and execute the programme. Finally I include the output generated by the programme.You need
--shell-escape
for the example to work.Other example
We can use this technique to include C code in a similar way (of course we need to use another compiler). In this example I exploit this to rapidly calculate factorials:
Now the same code with
lualatex
, which will yield the same output: