Skip to content

Commit 42ebb91

Browse files
committed
changed ndtm-marker to be displayed next to the state of the situation from which it is possible to choose from different alternative steps. added escaping of latex-reserved characters.
1 parent 8460cfd commit 42ebb91

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

src/de/moritzf/turingmachine/latex/LatexExporter.java

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public class LatexExporter {
3838
*/
3939
private static final String MULTIBAND_SEPARATOR = "\n$\\\\.....................\\\\$\n\n";
4040

41+
private static final String NDTM_MARKER = "_\\boxdot";
42+
4143
/**
4244
* Generate latex string representing the steps of a list of TuringSteps.
4345
*
@@ -62,11 +64,6 @@ public static String generateLatex(List<TuringStep> steps) {
6264
private static String generateSegmentForSingleBandContext(List<TuringStep> steps, int i) {
6365
String segment = "";
6466
TuringStep currentStep = steps.get(i);
65-
TuringStep previousStep = null;
66-
if (i > 0) {
67-
previousStep = steps.get(i - 1);
68-
69-
}
7067

7168

7269
String currentBand = currentStep.getBands().get(0);
@@ -78,10 +75,7 @@ private static String generateSegmentForSingleBandContext(List<TuringStep> steps
7875
if (!equalContent) {
7976
segment += "$\n\\begin{smallmatrix} \n";
8077
for (int j = 0; j < currentBand.length(); j++) {
81-
segment += currentBand.charAt(j);
82-
if (previousStep != null && currentStep.isNdtmStep() && j == previousStep.getPosition()[0]) {
83-
segment += "_\\boxdot";
84-
}
78+
segment += escapeChar(currentBand.charAt(j));
8579

8680
if (j < currentBand.length() - 1) {
8781
segment += " & ";
@@ -101,7 +95,10 @@ private static String generateSegmentForSingleBandContext(List<TuringStep> steps
10195

10296
if (j == currentPosition) {
10397
arrowPart += " \\uparrow ";
104-
statePart += currentState;
98+
statePart += escapeString(currentState);
99+
if (i < steps.size() - 1 && steps.get(i + 1).isNdtmStep()) {
100+
statePart += NDTM_MARKER;
101+
}
105102
}
106103

107104
if (j < currentBand.length() - 1) {
@@ -132,11 +129,6 @@ private static String generateSegmentForMultiBandContext(List<TuringStep> steps,
132129
TuringStep currentStep = steps.get(i);
133130
List<String> currentBands = currentStep.getBands();
134131
String currentState = currentStep.getState();
135-
TuringStep previousStep = null;
136-
if (i > 0) {
137-
previousStep = steps.get(i - 1);
138-
139-
}
140132

141133

142134
for (int j = 0; j < currentBands.size(); j++) {
@@ -148,11 +140,8 @@ private static String generateSegmentForMultiBandContext(List<TuringStep> steps,
148140

149141
segment += "[" + (j + 1) + "] & ";
150142
for (int k = 0; k < currentBand.length(); k++) {
151-
segment += currentBand.charAt(k);
143+
segment += escapeChar(currentBand.charAt(k));
152144

153-
if (previousStep != null && currentStep.isNdtmStep() && k == previousStep.getPosition()[j]) {
154-
segment += "_\\boxdot";
155-
}
156145

157146
if (k < currentBand.length() - 1) {
158147
segment += " & ";
@@ -167,7 +156,10 @@ private static String generateSegmentForMultiBandContext(List<TuringStep> steps,
167156
for (int k = 0; k < currentBand.length(); k++) {
168157
if (k == currentPosition) {
169158
arrowPart += " \\uparrow ";
170-
statePart += currentState;
159+
statePart += escapeString(currentState);
160+
if (i < steps.size() - 1 && steps.get(i + 1).isNdtmStep()) {
161+
statePart += NDTM_MARKER;
162+
}
171163
}
172164

173165
if (k < currentBand.length() - 1) {
@@ -190,4 +182,33 @@ private static String generateSegmentForMultiBandContext(List<TuringStep> steps,
190182
return segment;
191183
}
192184

185+
private static String escapeChar(char c) {
186+
String escapedChar = Character.toString(c);
187+
if (c == '&' || c == '%' || c == '$' || c == '#' || c == '_' || c == '{' || c == '}') {
188+
escapedChar = "\\" + escapedChar;
189+
190+
} else if (c == '~') {
191+
escapedChar = "\\sim";
192+
} else if (c == '^') {
193+
escapedChar = "\\wedge\n";
194+
} else if (c == '\\') {
195+
escapedChar = "\\backslash";
196+
}
197+
198+
return escapedChar;
199+
200+
}
201+
202+
203+
private static String escapeString(String text) {
204+
String escapedString = "";
205+
206+
for (int i = 0; i < text.length(); i++) {
207+
escapedString += escapeChar(text.charAt(i));
208+
}
209+
210+
return escapedString;
211+
212+
}
213+
193214
}

0 commit comments

Comments
 (0)