President Reagan ended a three-day state visit here today that appears to have left Spanish Prime Minister Felipe Gonzalez in a marginally better position to convince a doubting populace that Spain should remain part of the NATO alliance.

By agreeing at least to informal talks on lowering the U.S. military presence here the product of a 1953 bilateral accord, Reagan indirectly has boosted Gonzalez's argument that Spain needs to retain its multilateral defense ties.

Although it remains unclear whether the administration actually intends to negotiate a reduction in the 12,540 American troops here, and U.S. officials privately have said they consider every soldier vital, the Spanish government seemed to think it enough that the idea was not rejected out of hand.

But Gonzalez's problem is far from solved. Polls here consistently have shown Spaniards to be opposed by wide margins to both the American presence and to membership in the North Atlantic Treaty Organization.

And while Reagan's visit may have helped by showing the viability of Gonzalez's proposal to decrease one of those defense relationships, it may also have done some indirect damage to his hopes of maintaining the other.

Three days of protests organized by leftists, with banners reading "No to Reagan, U.S. forces and NATO," have served to focus public attention on a controversy that advisers to the socialist prime minister acknowledge is at least in part of his own making.

In 1982, Gonzalez was swept into office in a landslide electoral victory on what was seen as an anti-NATO platform.

Long isolated from the rest of Europe by geography, Spain after World War II was a dictatorship under Francisco Franco, politically cut off from a democratic Western Europe that saw no place for Madrid within the new economic union or western defense alliance.

Franco signed a bilateral pact with president Dwight D. Eisenhower that allowed American troops to be stationed at four Spanish bases, positioned to provide air, sea and communications support for U.S. forces in the Atlantic and Mediterranean.

Franco's death in 1975, and the installation of democracy here under a conservative government, brought new opportunities to become a full European partner.

In 1982, Spain joined NATO. There was little anti-alliance feeling then, according to Juan Diaz Nicolas, a sociologist and leading Spanish pollster. Then Gonzalez, the newly emergent leader of the Spanish Socialist Workers' Party, won the prime ministership on a platform charging that the Spanish people had not been consulted sufficiently on NATO by the conservatives.

It was a charge that had broad appeal in the newly democratic country where strains of individual independence, neutralism and leftism run deep. The argument was not that Spain should move from West to East but that it should stand above East-West quarrels.

Once in office, the Socialists froze negotiations then in progress over Spain's integration into NATO's military command structure.

As a result, the Spanish remain in a position akin to that of France. Spanish officials attend NATO ministerial meetings but NATO troops are not stationed here and Spain does not participate in NATO exercises.

Gonzalez promised a nationwide referendum over NATO membership, pledging to withdraw Spain from the alliance if the majority so wished. Advisers say Gonzalez was never anti-NATO, but merely pro-referendum. Yet the prime minister and others in the government, most prominently Deputy Prime Minister Alfonso Guerra, have allowed the widespread impression to persist that in their hearts they would prefer Spain out of the alliance.

Gonzalez, 43, frequently differentiates in public between decisions made "with the heart" and those made "with the head." After 2 1/2 years in office, the prime minister's head now clearly has concluded that taking Spain out of NATO would undermine Spanish efforts to enjoy some of the other fruits of western integration.

Principal among these is the European Community. After long, arduous negotiations with the Western Europeans, some of whom feared economically weaker Spain could be a burden, agreement was reached this spring on the admission of Spain and Portugal.

A number of European governments indicated it would be incongruous for Spain to join their economic club at the same time it sought to leave their defense alliance.

One senior aide to Gonzalez pronounced it "probable" that such leaders as Britain's Margaret Thatcher and West Germany's Helmut Kohl pushed for Spanish admission to the community only after Gonzalez assured them he would promote continued NATO membership.

Gonzalez has reiterated his pledge to hold the referendum, and government officials say it is scheduled tentatively for next spring. According to one of Diaz Nicolas' most recent polls, however, the 53 percent of Spaniards who would vote against NATO today would fall by only 6 percentage points in response to a direct pro-NATO request from Gonzalez.

Twenty-eight percent expressed no opinion. It is this portion of the electorate, plus a substantial portion of the antis, that Gonzalez must win over to produce a strong pro-NATO vote. His advisers say they believe the highly popular prime minister can win a pro-NATO referendum in the same way he won his majority in 1982.