# How to localize the symbols x,y, A, B in the following program? [closed]

I see a example in Paul Wellin's Programming with Mathematica An Introduction, 4th edition:

TruthTable[expr_, vars_] :=
Module[{len = Length[vars], tuples, rules, table,
tuples = Tuples[{True, False}, len];
rules = Thread[vars -> #1] & /@ tuples;
table = Transpose@Join[Transpose[tuples], {expr /. rules}];head=
Grid[Prepend[table /. {True -> "T", False -> "F"}, head],
Dividers -> {{1 -> {Thin, Black}, -1 -> {Thin, Black}, -2 -> {Thin,
LightGray}}, {1 -> {Thin, Black},
2 -> {Thin, LightGray}, -1 -> {Thin, Black}}},
BaseStyle -> {FontFamily -> "Times"}]];
Manipulate[
Module[{eqns = {(x - .5)^2 + y^2 < 1, (.5 + x)^2 + y^2 < 1},
c1 = {-.5, 0}, c2 = {.5, 0}},
Row[{(*IPM4Chap05Functional*)TruthTable[f[A, B], {A, B}],
Show[RegionPlot[f @@ eqns, {x, -2, 2}, {y, -2, 2}, Frame -> None,
PlotLabel -> f[A, B], PlotRange -> {{-2, 2}, {-1.2, 1.2}},
AspectRatio -> Automatic, MaxRecursion -> 5],
Graphics[{Circle[c1], Circle[c2], Text["A", {-0.75, .5}],
Text["B", {0.75, .5}]}], ImageSize -> Small]}]], {{f, Xor,
"Logical function"}, {And, Or, Xor, Implies, Nand, Nor}},
SaveDefinitions -> True]


I try as: TruthTable[expr_, vars_] := Module[{names, showNames, len = Length[vars], tuples, rules, table, head}, tuples = Tuples[{True, False}, len]; rules = Thread[vars -> #1] & /@ tuples; table = Transpose@Join[Transpose[tuples], {expr /. rules}]; names = Map[ToString, vars]; showNames = Map[StringSplit[#, "$"][[1]] &, names]; head = Append[showNames, StringReplace[ToString@TraditionalForm[expr], MapThread[#1 -> #2 &, {names, showNames}]]]; Grid[Prepend[table /. {True -> "T", False -> "F"}, head], Dividers -> {{1 -> {Thin, Black}, -1 -> {Thin, Black}, -2 -> {Thin, LightGray}}, {1 -> {Thin, Black}, 2 -> {Thin, LightGray}, -1 -> {Thin, Black}}}, BaseStyle -> {FontFamily -> "Times"}]] Manipulate[ Module[{A, B, x, y, eqns, c1 = {-.5, 0}, c2 = {.5, 0}, lgrid}, eqns = {(x - .5)^2 + y^2 < 1, (.5 + x)^2 + y^2 < 1}; lgrid = TruthTable[f[A, B], {A, B}]; Row[{(IPM4Chap05Functional)lgrid, Show[RegionPlot[f @@ eqns, {x, -2, 2}, {y, -2, 2}, Frame -> None, PlotLabel -> lgrid[1, 1, 3], PlotRange -> {{-2, 2}, {-1.2, 1.2}}, AspectRatio -> Automatic, MaxRecursion -> 5], Graphics[{Circle[c1], Circle[c2], Text["A", {-.9, .25}], Text["B", {.9, .25}]}], ImageSize -> Small]}]], {{f, Xor, "Logical function"}, {And, Or, Xor, Implies, Nand, Nor}}, SaveDefinitions -> True] It works. But I wonder any better solution? - ## closed as off-topic by m_goldberg, Öskå, bobthechemist, Kuba, Michael E2Jun 19 '14 at 16:06 • The question does not concern the technical computing software Mathematica by Wolfram Research. Please see the help center to find out about the topics that can be asked here. If this question can be reworded to fit the rules in the help center, please edit the question. It would be great if you could come up with a small working example. Your question as it states now is too localized IMO. – Öskå Jun 19 '14 at 12:09 Please properly format the code you added in your edit. Dumping unformatted code into a question will diminish your chances of getting an answer. – m_goldberg Jun 19 '14 at 12:15 This question appears to be off-topic because it is too localized; i.e, it applies only to the local situation and needs of its poster and answers will not benefit others. – m_goldberg Jun 19 '14 at 13:12 @m_goldberg - I don't understand the "system" here: You answer the question (see below), then you qualify the answer as "off-topic" (see above), and finally you vote to close it. I have observed similar "patterns" during the last days: Give an answer (and collect the pennies), then vote to put it on hold or even close it. – eldo Jun 19 '14 at 20:45 @eldo. I see no contradiction in my behavior. I answer the question because I want to help the OP. But neither my answer nor BobHanlon's are likely to help anyone other than the OP, so I nominate the question for closure as too localized. When the question is deleted, I'll loose the pennies, which bothers me not at all. – m_goldberg Jun 19 '14 at 20:58 ## 2 Answers I am guessing that your problem is that the plot label doesn't display correctly in your modified code. To make the result of your Manipulate expression, with A, B, x and y localized by Module, display correctly, try TruthTable[expr_, vars_] := Module[{names, showNames, len = Length[vars], tuples, rules, table, head}, tuples = Tuples[{True, False}, len]; rules = Thread[vars -> #1] & /@ tuples; table = Transpose@Join[Transpose[tuples], {expr /. rules}]; names = Map[ToString, vars]; showNames = Map[StringSplit[#, "$"][[1]] &, names];
Append[
showNames,
StringReplace[
MapThread[#1 -> #2 &, {names, showNames}]]];
Grid[Prepend[table /. {True -> "T", False -> "F"}, head],
Dividers ->
{{1 -> {Thin, Black}, -1 -> {Thin, Black}, -2 -> {Thin,LightGray}},
{1 -> {Thin, Black}, 2 -> {Thin, LightGray}, -1 -> {Thin, Black}}},
BaseStyle -> {FontFamily -> "Times"}]]

Manipulate[
Module[{A, B, x, y, eqns, c1 = {-.5, 0}, c2 = {.5, 0}},
eqns = {(x - .5)^2 + y^2 < 1, (.5 + x)^2 + y^2 < 1};
Row[{
TruthTable[f[A, B], {A, B}],
" ", (* space to get right side of frame to show *)
Show[
RegionPlot[f @@ eqns, {x, -2, 2}, {y, -2, 2},
Frame -> None,
PlotLabel -> f["A", "B"], (* use text elements to show variable names *)
PlotRange -> {{-2, 2}, {-1.2, 1.2}},
AspectRatio -> Automatic,
MaxRecursion -> 5],
Graphics[{Circle[c1], Circle[c2], Text["A", {-.9, .25}], Text["B", {.9, .25}]}],
ImageSize -> Small]}]],
{{f, Xor, "Logical function"}, {And, Or, Xor, Implies, Nand, Nor}},
SaveDefinitions -> True]


Note that I remove the local variable lgrid; there is no need for it.

-
When I execute your code the unique module variables (e.g., A\$204) show up in the table headings. –  Bob Hanlon Jun 19 '14 at 13:29
@BobHanlon. I used the OP's modified version of TruthTable, which although clunky, takes care of the problem you mention. I've added that version of TruthTable to my answer to make it self-contained. –  m_goldberg Jun 19 '14 at 20:01
A = 1; B = 2; x = 1; y = 2;  (* external variables *)

TruthTable[expr_, vars_] :=
Module[
{len = Length[vars], tuples, rules, table, head},
tuples = Tuples[{True, False}, len];
rules = Thread[vars -> #1] & /@ tuples;
table = Transpose@
Join[
Transpose[tuples],
{expr /. rules}];
Grid[
Prepend[
table /. {True -> "T", False -> "F"},
Dividers -> {
{1 -> {Thin, Black},
-1 -> {Thin, Black},
-2 -> {Thin, LightGray}},
{1 -> {Thin, Black},
2 -> {Thin, LightGray},
-1 -> {Thin, Black}}},
BaseStyle -> {FontFamily -> "Times"}]];

Manipulate[
Module[
{x, y, var = {"A", "B"}, (* x and y localized, text for A and B *)
eqns, c1 = {-.5, 0}, c2 = {.5, 0}},
eqns = {
(x - .5)^2 + y^2 < 1,
(.5 + x)^2 + y^2 < 1};
Row[{(*IPM4Chap05Functional*)
TruthTable[f @@ var, var],
Show[
RegionPlot[f @@ eqns,
{x, -2, 2}, {y, -2, 2},
Frame -> None,
PlotLabel -> f @@ var,
PlotRange -> {{-2, 2}, {-1.2, 1.2}},
AspectRatio -> Automatic,
MaxRecursion -> 5],
Graphics[{
Circle[c1],
Circle[c2],
Text[var[[1]], {-0.75, .5}],
Text[var[[2]], {0.75, .5}]}],
ImageSize -> Small]}]],
{{f, Xor, "Logical function"},
{And, Or, Xor, Implies, Nand, Nor}},
SaveDefinitions -> True]


{A, B, x, y} (* external variables unchanged *)


{1, 2, 1, 2}

-