President Reagan and Spanish Prime Minister Felipe Gonzalez tried today to smooth out differences and emphasize areas of agreement on the touchy issues of Central American policy and the U.S. military presence in Spain.

Reagan and Gonzalez agreed in principle to begin discussions at an unspecified time about renegotiation of an agreement under which 12,540 U.S. troops are stationed at four Spanish military bases. The two sides stressed their desire for peace in Central America and in effect agreed to disagree about their differing views of the leftist Nicaraguan government.

But the friendly visit was marred tonight by violence close to the U.S. Embassy as police made repeated charges to chase away about 2,000 protesters who had held up rush-hour traffic on a main Madrid artery for more than 90 minutes.

As the baton-wielding police moved against the demonstrators, they were pelted with rocks. Several demonstrators and police were injured and several demonstrators were arrested, according to police and Spanish news reports.

The demonstration was organized by a left-wing and union coalition that staged a massive protest Sunday. Police kept the protesters away from the embassy, where they wanted to deliver a letter calling for U.S. troop withdrawal from Spain and declaring Reagan "persona non grata."

As Reagan and Gonzalez discussed Nicaragua, senior Spanish officials said Spain has agreed to host a visit here this weekend by Nicaraguan President Daniel Ortega in response to an urgent Nicaraguan request that followed western criticism of Ortega's current 12-day tour of the Soviet Bloc. Details on Page A25.

In statements issued after a working lunch, Reagan and Gonzalez acknowledged "differences" on the question of Nicaragua. Spanish sources said Gonzalez had appealed for continued support for the Contadora peace process developed by four Latin American states and that Reagan reiterated his public support of that effort.

But they said that Gonzalez's request for a resumption of U.S.-Nicaraguan bilateral talks broken off by Washington in January had met with "no flexibility" in the U.S. position.

The officials said that disapproval of U.S. policy and Gonzalez's agreement to meet with Ortega did not indicate Spanish support for the policies of Nicaragua's Sandinista government.

But they said Gonzalez felt Ortega's request was one he could not refuse considering Spain's belief that it can play a role in obtaining a peaceful solution to the conflicts in Central America.

Despite evidence of U.S. inflexibility, they said, Gonzalez wants "to see if there is any possible way to repair the lines of dialogue" between Washington and Managua.

Spanish officials had made it clear that they expected U.S. policy in Nicaragua to be a primary topic in conversations between Gonzalez and Reagan and also in complementary talks here between Secretary of State George P. Shultz and Spanish Foreign Minister Fernando Moran.

In a news conference following several bilateral sessions, Shultz said both governments "feel that it is important to have democracy and the rule of law in Central America."

But Shultz, who increasingly has indicated his own problems with the sanctions decision in comments over the past several days, said "I did not get any sense of support for our economic sanctions" from the Gonzalez government.

Noting the U.S. belief that Nicaragua "is definitely moving itself in the Soviet and totalitarian direction," Shultz acknowledged that "the Spaniards questioned the degree to which that's so."

"It seems pretty clear to us," Shultz said. "And given that difference in analysis, it leads you to somewhat different policy conclusions, although we agree on the importance, in the end, of democratic pluralism in Nicaragua as an essential ingredient to stability in Central America."

In answer to a question, Shultz said the United States has not sought mediation "in any way" between itself and the Sandinistas.

Moran, in a separate news conference, restated those areas of agreement cited by Shultz, and noted that Spain placed "a different emphasis" on some areas of the Nicaragua problem, and disagreed over its roots.

A senior Spanish official with close ties to Gonzalez said, after the bilateral talks, "I had the feeling that the United States has a predetermined, fixed policy. There was no sense that there is going to be any change there."

At the same time, he said, the United States "clearly is in the position now to tighten the noose" around Nicaragua.

Tonight, Reagan paid tribute to Spain in a toast at a state dinner hosted by King Juan Carlos at the Pardo Palace, two miles from the violence at the U.S. Embassy.

"Today Spain is moving forward in a voyage of freedom and democracy every bit as courageous as that of Columbus," Reagan said, in words that echoed the conciliatory and positive tone both sides were using in their talks in an effort to avoid controversies.

Spanish officials expressed pleasure with the talks, including the U.S. promise to begin discussing the troop reduction issue. Publicly and privately, they characterized the U.S. attitude as one of intense curiosity over the Spanish position. They said the United States appeared to be relieved that the Gonzalez government was not pressing for immediate renegotiation and was willing to settle for the initiation of informal talks.

"The Reagan visit was not the occasion to establish negotiations," Moran told reporters. "It was a formal visit."

Instead, he said, the two governments would begin informal diplomatic conversations "to clarify the political and technical positions" of the two sides and establish "the conditionalities" for formal negotiations.

Shultz said the informal diplomatic talks would move ahead with "the broad subject of the strategic role of Spain, its defensive needs, its relationship to our strategy and NATO."

The current agreement under which U.S. troops are stationed here is the extension of an accord signed in 1953 by Dwight D. Eisenhower and Spanish ruler Francisco Franco. The Gonzalez government signed a five-year extension of the agreement in 1983.

Gonzalez, whose Socialist Party was elected by a landslide in 1982, has proposed reductions in the number of U.S. troops here as an alternative to carrying out his popular campaign promise to support Spain's withdrawal from NATO.

Spain's rationale, as expressed in talks with U.S. officials, is that the special conditions that existed when the original agreement was signed in 1953 no longer apply. They cite the death of Franco and the establishment of democracy here, Spain's membership in NATO since 1982 and this spring's approval of membership in the European Common Market as changes that have made the current level of the U.S. military presence unnecessary.

The U.S. position is that western security still requires a major U.S. presence, a view Reagan stressed this morning in a speech to Spanish community leaders.

"Now Spain is an important partner in the free alliance of European democracies, the North Atlantic Treaty Organization that has protected our liberties and kept the peace for almost 40 years -- the longest period of peace Europe has known since the Roman Empire," Reagan said. "And we need Spain."

Although apparently content with today's agreement to open a "diplomatic channel" to discuss future formal negotiations, one senior Spanish official close to Gonzalez said he was under no illusion that these talks would be easy.

But this official characterized the visit as "smooth," saying that after Reagan's controversial trip to West Germany, the Americans seemed anxious to avoid conflict.

In his speech to the community leaders today Reagan continued to celebrate the virtues of democracy and private enterprise, the principal themes of an address he made Monday in West Germany.

"I'm convinced that historians will look back on Iberia's peaceful and joyful embrace of democracy as a decisive turning point," Reagan said. "They will see it as the moment when freedom ended a long retreat and began a broad, new advance that has spread from Spain and Portugal to the Americas and has, in one short decade, brought over 225 million people into the family of free nations."

Reagan said there are only four exceptions to "the democratic tide in Spanish-speaking America" -- Paraguay and Chile, which he called victims of "entrenched military rule" and Cuba and Nicaragua, which he listed as "communist tyrannies."